1
我有一個煩人的情況,即使我完全信任地運行代碼,方法的後期綁定調用拋出MethodAccessException。情況如下:私有成員的後期綁定拋出一個MethodAccessException
我有一個基類,它映射一些事件按照慣例處理邏輯,這些處理程序是使用通過發射IL代碼創建的動態方法調用,以下這個教程:http://www.codeproject.com/KB/cs/dynamicmethoddelegates.aspx
//in AssemblyA.dll:
public abstract class Base : IEventHandler
{
protected static void RegisterDerivedType(Type derived)
{
//create list of delegates to invoke event handlers
}
void IEventHandler.Handle(IEvent e)
{
//late bound invocation of appropriate handler method (e.g. EventX
//or EventY)
//this code throws a MethodAccessException
}
}
//in assemblyB.dll
public class Derived : Base
{
static Derived()
{
RegisterDerivedType(typeof(Derived));
}
private void OnEventX(EventX e) //EventX is derived of IEvent
{ }
private void OnEventY(EventY e) //EventY is derived of IEvent
{ }
}
爲什麼我不可能用動態方法調用私人成員?