2

I have ApiContext object (do not have the code) and would like to make extention method for it

what is wrong with the code? it says:

cannot use this in static member

ApiContext resides in the Singleton Pattern

public sealed class EbayProxySdk
{
    private static EbayProxySdk _instance = null;
    private static readonly Object LockObj = new object();

    public ApiContext Context;
.
.
.
}

public static class MyExtensions
{
    public static ApiContext DeepClone(this ApiContext context)
    {
        ApiContext other = (ApiContext)this.MemberwiseClone();
        return other;
    }
}   

EDIT what I am eventually trying to do is Deep Clone to ApiContext which is a complex object with nested objects

nvoigt
  • 75,013
  • 26
  • 93
  • 142
user829174
  • 6,132
  • 24
  • 75
  • 125

2 Answers2

1

You cannot call MemberwiseClone from outside the class because it is a protected method. If you could do that than anyone could call any protected method which is not in the spirit of access protection. Can't be done.

You can use (full trust) reflection to do this or you can make the class ApiContext cooperate by exposing a public method that does what you want.

usr
  • 168,620
  • 35
  • 240
  • 369
  • Can you please share code example how can Deep Clone this object? it has nested objects inside as well – user829174 Aug 09 '15 at 11:33
  • I will not write your code for you but I'll help you with any concrete problems. Did you understand what I said in this answer? – usr Aug 09 '15 at 11:34
  • But that's not the reason why his code is failing, is it? The reason is that he is trying to access `this` in static context which is impossible. – Andrew Savinykh Aug 09 '15 at 11:38
  • @zespri there is no good workaround. Even if he changes this to context he gains nothing. It's just a different error message. – usr Aug 09 '15 at 11:39
-1

Well, you cannot use this in a static method. Use the variable you named context:

public static class MyExtensions
{
    public static ApiContext DeepClone(this ApiContext context)
    {
        ApiContext other = (ApiContext)context.MemberwiseClone();
        return other;
    }
} 
nvoigt
  • 75,013
  • 26
  • 93
  • 142
  • 1
    now it sais 'cannot access protected method MemberWiseClone here' – user829174 Aug 09 '15 at 11:29
  • Then you cannot call it. An extension method cannot do anything special, you cannot access methods or fields you would not normally have access to. Extension methods are just syntactic sugar, they do not break the access modifier rules. – nvoigt Aug 09 '15 at 11:30
  • Can you please share code example how can Deep Clone this object? it has nested objects inside as well – user829174 Aug 09 '15 at 11:33
  • @user829174 There is no single best way to do this. Maybe you want to ask another question from here, with more info about how this object looks like? Does it implement IClonable? ISerializable? DataContract? None of the above? – nvoigt Aug 09 '15 at 11:35
  • Maybe it's easier to create a second one from parameters the first can deliver, than clone it? You need to provide the object description. My guess would be that you are trying to solve an XY problem. This object does not seem to support deep cloning in any way. Maybe that's for a reason. If you are the first and only programmer that needs to deep clone it, maybe the others chose a different way? – nvoigt Aug 09 '15 at 11:38