简体   繁体   English

覆盖第三方库的私有方法

[英]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? 有什么方法可以覆盖Calculate()方法吗?

Reason: Inside Calculate() method I need to modify some logic 原因:在Calculate()方法内部,我需要修改一些逻辑

No, there's no way in C#. 不,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. 1)创建一个扩展计算器类的类。

2) Override Execute method and reuse part of the code you have in Calculator class. 2)重写Execute方法并重用您在Calculator类中拥有的部分代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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