2009-04-10 3 views

답변

3

Reflection.Emit으로 말합니까? 그렇다면 TypeBuilder.DefineMethodMethodAttributes.Abstract을 사용합니다.

다음은 예입니다. Bar.Methodabstract이고; Bar2.Method이이를 대체합니다.

AssemblyName an = new AssemblyName("Foo"); 
    var asm = AppDomain.CurrentDomain.DefineDynamicAssembly(an, AssemblyBuilderAccess.Run); 
    var module = asm.DefineDynamicModule("Foo"); 
    var type = module.DefineType("Bar", TypeAttributes.Abstract | TypeAttributes.Class | TypeAttributes.AnsiClass); 
    var method = type.DefineMethod("Method", MethodAttributes.Abstract | MethodAttributes.Public | MethodAttributes.Virtual, 
     CallingConventions.HasThis, typeof(int), Type.EmptyTypes); 

    var final = type.CreateType(); 

    type = module.DefineType("Bar2", TypeAttributes.Sealed | TypeAttributes.Class | TypeAttributes.AnsiClass, final); 
    var method2 = type.DefineMethod("Bar", MethodAttributes.Public | MethodAttributes.Virtual, 
     CallingConventions.HasThis, typeof(int), Type.EmptyTypes); 
    var il = method2.GetILGenerator(); 
    il.Emit(OpCodes.Ldc_I4_4); 
    il.Emit(OpCodes.Ret); 
    type.DefineMethodOverride(method2, method); 

    var concrete = type.CreateType(); 
    object obj = Activator.CreateInstance(concrete); 
    int result = (int) concrete.GetMethod("Bar").Invoke(obj, null); 
+0

아, 그게 그 질문의 의미 일 가능성이 더 큽니다. 유용하게 밝혀 졌을 때를 대비하여 대답을 남겨 둘 것입니다.하지만 이걸 가지고 있다고 생각합니다 :) –

0

CodeDOM을 의미합니까? 반사 새 코드를 기존 코드를 읽는 데 사용됩니다.

당신이이 된 CodeDom 의미합니까 경우

, 나는 그냥 CodeMemberProperty을 만들고 MemberAttributes.Abstract을 포함하도록 Attributes 속성을 설정해야합니다 생각합니다.

관련 문제