none
IL dynamically generated proxy class RRS feed

  • Question

  • public Interface ICalculate{
    int Add(int x,int y);
    }
    I want to generated the following csharp code:
    public class CalcService:ICalculate{
    public int Add(int x,int y){
    return x+y;
    }
    }
    IL:
    public static object CreateService(){
    var assemblyName = new AssemblyName("XTFS");
    var assemblyBuilder=AppDomain.CurrentDomain.DefineDynamicAssembly(assemblyName, AssemblyBuilderAccess.Run);
    var moduleBuilder = assemblyBuilder.DefineDynamicModule("XTFSModel");
    // create type builder for a class 
    var typeBuilder = moduleBuilder.DefineType("XTFSClass", TypeAttributes.Public,typeof(object),new Type[]{typeof(ICalculate)});
    var methodBuilder = typeBuilder.DefineMethod(
    "Add",
    MethodAttributes.Public ,
    typeof(int),
    new Type[]{typeof(int),typeof(int)});
    var il = methodBuilder.GetILGenerator();
    il.Emit(OpCodes.Ldarg_1);
    il.Emit(OpCodes.Ldarg_2);
    il.Emit(OpCodes.Add);
    il.Emit(OpCodes.Ret);
    Type objeType = typeBuilder.CreateType();
    return Activator.CreateInstance(objeType);
    }

    Error Display Add method is not implemented
    Friday, December 22, 2017 9:53 AM

Answers

All replies