如何使用反射调用被派生类重写的基本方法?
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();
}
Run Code Online (Sandbox Code Playgroud)
此代码始终显示"派生"...
在cil代码中,ldftn用于获取函数指针地址以调用委托构造函数(即.ctor(object,native int)).
如何在C#中获取用于构造委托的函数指针?
如何在.NET中运行静态构造函数(即模块构造函数和类型构造函数)?
例如:
class A
{
static A()
{
Environment.Exit(0);
}
static int i()
{
return 100;
}
}
Run Code Online (Sandbox Code Playgroud)
如何在i()
不退出的情况下调用?