2011-12-12 2 views
1

완전히 신뢰할 수있는 코드를 실행하더라도 메서드의 런타임에 바인딩 호출이 MethodAccessException을 throw하는 성가신 상황이 있습니다.전용 멤버의 후기 바인딩 호출로 MethodAccessException이 throw됩니다.

나는 규칙에 따라 처리 논리를 일부 이벤트를 매핑하는 기본 클래스를 가지고

는,이 핸들러는 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 
    { } 
} 

다음과 같은 상황입니다 동적 메서드로 비공개 멤버를 호출 할 수없는 이유는 무엇입니까?

답변

3

DynamicMethod은 여전히 ​​규칙을 따릅니다. 구부리기 위해서는 owner 매개 변수를을 선언하는 유형으로 지정해야합니다 (DynamicMethod을 작성할 때). 그때 당신은 당신이 그 유형 안에있는 것처럼 효과적으로 달리고 있습니다. 그래서 :

var method = new DynamicMethod( name, returnType, parameterTypes, declaringType, true); 

(또는 owner 동의 유사한 오버로드의)

관련 문제