[英]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 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.