ShaneKm
ShaneKm

Reputation: 21338

Overwrite private method of 3rd party library

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

Upvotes: 2

Views: 1470

Answers (1)

Francisco Goldenstein
Francisco Goldenstein

Reputation: 13767

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.

Upvotes: 5

Related Questions