简体   繁体   English

覆盖非派生c#类中的函数

[英]Override function in non derived c# class

I have the following class 我有以下课程

public class classB : classA 

which contains function dostuff 其中包含函数dostuff

Now I have a class SUPER , which has an instance of B amongst its variables 现在我有一个SUPER类,它的变量中有一个B实例

classB SUPER_B;

and that has a 'redefinition' of 'dostuff' amongst its methods. 并且在其方法中有'重新定义''dostuff'。 Basically, what I need is that when classB calls 'dostuff', it does not call its internal dostuff, but SUPER's dostuff implementation instead. 基本上,我需要的是当classB调用'dostuff'时,它不会调用它的内部dostuff,而是调用SUPER的dostuff实现。

Substantially, I would need that, on SUPER's initialization, classB's 'dostuff' be replaced with SUPER's dostuff. 基本上,我需要在SUPER的初始化时,将classB的'dostuff'替换为SUPER的dostuff。

It'd sort of make sense if there was a way to retrieve B's 'dostuff''s reference, but I don't know how to achieve that. 如果有一种方法可以检索B的'dostuff'的参考,那有点意义,但我不知道如何实现这一点。

I've looked into virtual and Func, but they seem to require that SUPER be a derivate of B 我调查了虚拟和Func,但他们似乎要求SUPER是B的衍生物

You cannot force a method change upon a class from the outside. 您不能从外部强制对类进行方法更改。 There are ways to achieve functionality like that when either (1) classB is designed in such a way as to support replacing its methods, eg though a settable delegate, or (2) if classB implements an interface, which you can implement, wrap classB inside it, and replace a method. 当(1)classB以支持替换其方法的方式设计时,有很多方法可以实现这样的功能,例如通过可设置的委托,或者(2)如果classB实现了一个可以实现的接口,则包装classB在里面,并替换一个方法。

Example #1 - settable delegate: 示例#1 - 可设置的委托:

class ClassB {
    public Action<int> dostuff {get;set;}
    public ClassB() {
        dostuff = myDoStuff;
    }
    void myDoStuff(int n) {
    }
}

class Program {
    public static void Main() {
        var myB = new ClassB();
        myB.dostuff = myOverride;
    }
    void myOverride(int n) {
    }
}

Example #2 - interfaces. 示例#2 - 接口。

interface IClassB {
    void dostuff(int n);
    void dosomething();
}
class ClassB : IClassB {
    void dostuff(int n) {
    }
    void dosomething() {
    }
}
class Program {
    public static void Main() {
        var bb = new Override(new ClassB());
    }
    class Override : IClassB {
        private readonly IClassB inner;
        public Override(IClassB inner) {
            this.inner = inner;
        }
        void dostuff(int n) {
            // do not call inner.dostuff
        }
        void dosomething() {
            inner.dosomething();
        }
    }
}

It sounds like you need to use decorator pattern: class SUPER has an instance of classB and uses it's methods. 听起来你需要使用装饰器模式:类SUPER有一个classB的实例并使用它的方法。 SUPER decorates all necessary methods of classB , and adds some new. SUPER装饰了classB所有必要方法,并添加了一些新方法。 For example: 例如:

public class SUPER {
  private classB SUPER_B = new classB();
  public void foo()  {
     SUPER_B.foo();
  }
  public void bar()  {
     SUPER_B.bar();
  }
  ...
  public void dostuff()
  {
       // don't call SUPER_B.bar(), but write your implementation
  }
}

You can't do that easily. 你不能轻易做到这一点。

Instead you should design your classB for extensibility, such as add events to classB which you can handle in your SUPER class. 相反,您应该设计classB以实现可扩展性,例如将事件添加到您可以在SUPER类中处理的classB

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

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