我有一个烦人的情况,即使我完全信任地运行代码,方法的后期绑定调用也会引发 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
{ }
}
为什么我不能用动态方法调用私有成员?