我如何在.NET中引用特定的基类方法?

时间:2014-05-03 02:48:27

标签: c# .net c#-4.0

在.NET中我试图修补第三方库。我创建了一个新类Grandchild,它继承自Parent派生的类Child。在Grandchild的覆盖方法中,我需要跳过Child并调用Parent。那可能吗?换句话说,我想做:

public class Grandchild : Child {
    public void override MyMethod() {
        // illegal syntax examples, but how can I invoke Parent.MyMethod?
        base.base.MyMethod();  // nope...
        Parent.MyMethod(); // nope...
    }
}

1 个答案:

答案 0 :(得分:6)

这根本无法完成,甚至不能用反射,并且出于一个很好的理由 - 安全。

如果您分发的类强制执行某些业务规则,并且客户端能够跳过这些规则,则可能会遇到巨大的安全漏洞。这种基本的多态性规则根本不能违反。

安全性也是表达式树不能包含对base的引用的原因,正如Eric Lippert here所解释的那样。

//this is illegal
Expression<Func<string>> nonVirtualCall = () => base.Method();
相关问题