简体   繁体   English

派生类的C#方法作为基础构造函数中的委托

[英]C# method from derived class as delegate in base constructor

Why is the following C# not legal? 为什么以下C#不合法? Does there exist a proper workaround? 是否存在正确的解决方法?

public class Base
{
    public Base(Func<double> func) { }
}

public class Derived : Base
{
    public Derived() : base(() => Method()) <-- compiler: Cannot access non-static method 'Method' in static context
    {
    }

    public double Method() { return 1.0; }
}

It's effectively referring to "this" within the arguments to the base constructor, which you can't do. 它在基础构造函数的参数中有效地引用了“this”,这是你不能做到的。

If your delegate really doesn't need access to this (which your sample doesn't) you can just make it static. 如果你的代表真的不需要访问this (你的样本没有),你可以让它静态。 You could also use a method group conversion to make it simpler: 您还可以使用方法组转换使其更简单:

public class Base
{
    public Base(Func<double> func)
    {
        double result = func();
    }
}

public class Derived : Base
{
    public Derived() : base(Method)
    {
    }

    public static double Method() { return 1.0; }
}

If you do need to use "this", you could: 如果你确实需要使用“this”,你可以:

  • Make it a virtual method instead of calling a delegate 使其成为虚拟方法,而不是调用委托
  • Make it a static method which takes an appropriate instance, eg 使它成为一个静态方法,它采用适当的实例,例如

     public class Base { public Base(Func<Base, double> func) { double result = func(this); } } public class Derived : Base { public Derived() : base(x => Method(x)) { } private static double Method(Base b) { // The documentation would state that the method would only be called // from Base using "this" as the first argument Derived d = (Derived) b; } } 

Another solution to this is simply to defer the initialization of the delegate to the derived class: 另一个解决方案是将委托的初始化推迟到派生类:

public class Base {
   protected Func<double> DoubleFunc { get; set; }

   protected Base() {
      // defer initialization by not setting DoubleFunc
      // or another possibility is to set it to a dummy function:
      DoubleFunc = () => 0;
   }

   public Base(Func<double> func) {
      DoubleFunc = func;
   }
}

public class Derived : Base {
   public Derived() {
      DoubleFunc = Method;
   }

   public double Method() { return 1.0; }
}

Basically you get the compiler error because you are referencing the instance-method Method without an instance of your class Derived . 基本上,您会收到编译器错误,因为您在没有类Derived实例的情况下引用了instance-method Method When calling base , the constructor has not yet finished and you don't have an instance of your class yet. 调用base ,构造函数尚未完成,您还没有类的实例。 If you made Method static it would work just fine. 如果你做Method static它会工作得很好。

Have you tried making Method() static as the compiler indicates? 您是否尝试过编译器指示的Method()静态? The problem is that an instance of Derived isn't available until after the constructor has returned so you can't call an instance method on it. 问题是Derived的实例在构造函数返回之后才可用,因此您无法在其上调用实例方法。

The "workaround" would be to make Method() a static method. “解决方法”是使Method()成为静态方法。

I can't explain the technical reasons why this does not work but essentially you're trying to call a method on an instance which does not yet exist. 我无法解释为什么这不起作用的技术原因,但实际上你试图在一个尚不存在的实例上调用一个方法。 How could it possibly work? 怎么可能有用呢?

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

相关问题 使用构造函数将委托方法从派生注入基类 - Inject delegate method from derived to base class using constructor 如何从 C# 中的派生类实例调用基方法? - How to call base method from derived class instance in C#? 从基本构造函数C#调用基类重写方法 - Call base class override method from base constructor C# 在C#中从派生类调用私有基方法 - Calling a private base method from a derived class in C# 在C#中如何从派生类实例调用已经在派生类中覆盖的基类方法 - in C# how to invoke base class method, which already override in derived class, from derived class instance 您能否将派生类添加到其基类列表中,然后从C#中的基类列表中调用派生类的方法 - Can you add a Derived Class to a list of its base class then call a method of the Derived class from the list of base class in C# C#派生类和基本构造函数参数逻辑 - C# derived class and base constructor parameter logic C# - 让所有派生类都调用基类构造函数 - C# - Making all derived classes call the base class constructor 继承基构造函数参数<summary>在派生类 C# - Inheriting base constructor parameter <summary> in a derived class C# C#根据参数类型实例化基础构造方法的派生类 - C# Instantiate derived class from base constructor based on parameter type
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM