如何使用反射调用派生类重写的基本方法?使用反射调用重写的基本方法
class Base
{
public virtual void Foo() { Console.WriteLine("Base"); }
}
class Derived : Base
{
public override void Foo() { Console.WriteLine("Derived"); }
}
public static void Main()
{
Derived d = new Derived();
typeof(Base).GetMethod("Foo").Invoke(d, null);
Console.ReadLine();
}
该代码始终显示“已得到” ......
我创建从@低于格特的答案将允许你这样做的延伸`typeof(Base).GetMethod(“Foo”)。InvokeNotOverride(d,null);`。你可以在这里找到它:http://www.simplygoodcode.com/2012/08/invoke-base-method-using-reflection.html – 2012-08-13 19:18:02