2

Is there a way to overwrite a private method in a third party library?

for example I have a class like so:

// Decompiled source
public class Calculator{
   protected override Status Execute(){
      this.Calculate();
      ...
      return Status.Ok;
   }

   private int Calcualte(){
       // need to overwrite this method
   }
}

Is there any way to overwrite Calculate() method?

Reason: Inside Calculate() method I need to modify some logic

ShaneKm
  • 20,823
  • 43
  • 167
  • 296
  • 4
    You can, but it is a real PITA to do it (It will likely involve [IL rewriting](http://www.mono-project.com/docs/tools+libraries/libraries/Mono.Cecil/faq/)). Are absolutely sure you actually need to do this? This sounds like a [XY Problem](http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem), there may be a much easier way to achieve the same same end goal. Perhaps explain what you are trying to do that overwriting a private method is the solution to that problem? – Scott Chamberlain Feb 12 '16 at 17:22
  • duplicated: http://stackoverflow.com/questions/1451787/overriding-a-private-method-with-reflection – Marcelo De Zen Feb 12 '16 at 17:30
  • You misunderstood my point. What are you doing that requires you to modify the logic to `Calculate()`? Perhaps you could make a derived class, perhaps you could make your own version of the file, we don't know what you could do if you don't tell us what you are trying to do. Pretend the answer to this question is "You can't do it, you need to find somthing else", add what you would of written in the body of your next question here of "I am trying to do _____ but can't edit a 3rd party class, what should I try next?" as a edit to this question. – Scott Chamberlain Feb 12 '16 at 17:35
  • Calculator class has bunch of other methods, fields, properties etc. There is only 1 line of code in Calculate() method that needs to be modified-I was trying to avoid creating a new class with exact same code with only 1 line modified. – ShaneKm Feb 12 '16 at 18:00

1 Answers1

4

No, there's no way in C#. A private method is only available in that class. When something is defined as private it's because is internal to that class. What you could do is:

1) Create a class that extends Calculator class.

2) Override Execute method and reuse part of the code you have in Calculator class.

Francisco Goldenstein
  • 13,299
  • 7
  • 58
  • 74
  • 2
    I almost want to give you a -1 for the absolute statement "No, there's no way". You can do it with IL rewriting using a library like [Mono.Cecil](http://www.mono-project.com/docs/tools+libraries/libraries/Mono.Cecil/faq/) but it is not a easy thing to do and leads to fragile code. – Scott Chamberlain Feb 12 '16 at 17:26
  • 4
    I meant, you cannot do it in C#, you could do IL rewriting or you could get the source code and change it. For me, if someone asks "can I override a private method?" the answer is "no, you can't and you shouldn't think about doing that". When something is defined as private it's because it's internal to that class. – Francisco Goldenstein Feb 12 '16 at 17:48