How to call base method from derived class instance in C#?

c#, inheritance

Solution

There is no way to access base method from outside the derived class. You can either write a method in derived class that will call base method like this:

public class B : A
{
    public override void print() { Console.WriteLine("B"); }

    public void basePrint() { base.print(); }
}

Or you can user `Reflection` to get base method definition and invoke it but it's rather ugly. Here is how to create `DynamicMethod` that calls base `print` method:

// Get MethodInfo of the base method
var methodInfo = typeof(A).GetMethod("Print", BindingFlags.Instance | BindingFlags.Public);

// Create DynamicMethod based on the methodInfo
var dynamicMethod = new DynamicMethod("BasePrint", methodInfo.ReturnType, new[] { methodInfo.DeclaringType }, methodInfo.DeclaringType);
// Create ILGenerator for the dynamic method
var il = dynamicMethod.GetILGenerator();

// Emit argument with index 0 - DeclaringType
il.Emit(OpCodes.Ldarg_0);
// Emit method call
il.EmitCall(OpCodes.Call, methodInfo, null);
// Emit method return value
il.Emit(OpCodes.Ret);

// Invoke method with target...
var b = new B();
dynamicMethod.Invoke(null, new object[] {b});

// ... or create a delegate and invoke method without target
var action = dynamicMethod.CreateDelegate(typeof(Action)) as Action;
action.Invoke();

Note that it'll work only for parameterless method. If you want to call method with parameters you'll have to put their types into the array with the `DeclaringType` and later emit all of them. Also you'll have to create delegate of type `Action<parameterTypes` or `Func<returnType, parameterTypes>` if the method returns something.

Problem

I have C# code, base `class A` and derived `class B`: ``` public class A { public virtual void print() {Console.WriteLine("a"); } } public class B:A { public override void print() { Console.WriteLine("b"); } } static void Main(string[] args) { A a= new B(); //In c++ I can use .Base:: to call base method from derived class instance a.Base::print(); } ``` I can't modify these two classes and I don't know what can I do in C#, any suggestion? Additionally, thanks to everyone joining into this discussion, and I would like to clarify why I need this behavior: In .net framework, we have an interface IPostBackDataHandler to handle postback. and in there is a method ``` public bool LoadPostData( string postDataKey, NameValueCollection postCollection ) ``` When I implement it and test, I find sometimes the given postback type of postCollection is NameValueCollection ,while other time it's HttpValueCollection (a derived class of NameValueCollection) Then,if it is a type of HttpValueCollection, when I get item from it, eg. postCollection['ControlID'] and I input html in this control, HttpValueCollection.get_item() will always validate the input and view it as a defect. While NameValueCollection.get_item() won't I don't want it operate validation automatically, at least I should decide whether it should be validated, should I?

Original source

Related problems