简体   繁体   中英

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM