2

In .NET I am trying to monkey-patch a 3rd party library. I created a new class, Grandchild, which inherits from a class Child derived from Parent. In an override method of Grandchild, I need to skip Child and invoke Parent. Is that possible? In other words, I want to do:

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...
    }
}
Scott Stafford
  • 43,764
  • 28
  • 129
  • 177

1 Answers1

6

That simply cannot be done, not even with reflection, and for a very good reason - security.

If a class you distributed enforced some business rules, and clients were able to skip those rules, you could run into a huge security hole. Such basic polymorphism rules simply cannot be violated.

Security is also why expression trees cannot contain references to base, as explained by Eric Lippert here.

//this is illegal
Expression<Func<string>> nonVirtualCall = () => base.Method();
Community
  • 1
  • 1
dcastro
  • 66,540
  • 21
  • 145
  • 155
  • The answers to the duplicate question give several methods for doing it. There are no direct ways, but it is possible by one of several workarounds. – Scott Stafford May 06 '14 at 02:13