2012-03-13 3 views
1

이유는 무엇과 같이 코드 : 는 sayHello가 '에 대한 정의를 포함하지 않는'객체 'C# dynamic + System.Reflection.Emit = 혼합하지 않습니까?

처리되지 않은 예외 : Microsoft.CSharp.RuntimeBinder.RuntimeBinderException

using System; 
using System.Reflection; 
using System.Reflection.Emit; 

class Program 
{ 
    static Type CreateDynamicType() 
    { 
    var typeBuilder = AppDomain.CurrentDomain 
     .DefineDynamicAssembly(
     name: new AssemblyName("FooAssembly"), 
     access: AssemblyBuilderAccess.Run) 
     .DefineDynamicModule("FooModule") 
     .DefineType("Foo", TypeAttributes.Class); 

    typeBuilder 
     .DefineDefaultConstructor(MethodAttributes.Public); 

    var method = typeBuilder 
     .DefineMethod("SayHello", MethodAttributes.Public); 

    var il = method.GetILGenerator(); 
    il.EmitWriteLine("Hello!"); 
    il.Emit(OpCodes.Ret); 

    return typeBuilder.CreateType(); 
    } 

    static void Main() 
    { 
    var type = CreateDynamicType(); 
    dynamic instance = Activator.CreateInstance(type); 
    instance.SayHello(); 
    } 
} 

예외를 생성합니다 '

하지만 reflection API를 통한 호출은 완벽하게 작동합니다. 어떤 아이디어?

답변

5

dynamic 다른 어셈블리의 내부 형식 멤버는 확인되지 않습니다.
(컴파일러와 똑같지 않음)

유형을 공개로 설정하십시오.

+0

좋은 캐치, 고마워요! – ControlFlow