C# 通过反射创建类的实例。Emmit

C# 通过反射创建类的实例。Emmit,c#,this,instance,cil,reflection.emit,C#,This,Instance,Cil,Reflection.emit,我正试图使用System.Reflection.Emit创建这个C#类 私有类MyTestData:IMyClonable { 私有int_testValue=0; 公共int测试值 { 获取{return\u testValue;} 设置{u testValue=value;} } 公共不可循环克隆() { MyTestData克隆=新建MyTestData(); 克隆。_testValue=_testValue; 返回克隆; } } 必须从此接口创建此类: 公共接口IMyTestData:

我正试图使用System.Reflection.Emit创建这个C#类

私有类MyTestData:IMyClonable
{
私有int_testValue=0;
公共int测试值
{
获取{return\u testValue;}
设置{u testValue=value;}
}
公共不可循环克隆()
{
MyTestData克隆=新建MyTestData();
克隆。_testValue=_testValue;
返回克隆;
}
}
必须从此接口创建此类:

公共接口IMyTestData:IMyClonable
{
int testValue{get;set;}
}
我已经编写了生成属性的代码,这项工作很好。但我在尝试创建方法Clone()时遇到了问题。我不知道如何创建此类本身的实例并将其保存在局部变量中。 以下是生成方法Clone()的代码:

private static void MakeCloneMethod(Type componentType,TypeBuilder-TypeBuilder)
{
构造函数生成器构造函数=
typeBuilder.DefineDefaultConstructor(MethodAttributes.Public);
MethodInfo cloneMethod=typeof(IMyClonable).GetMethod(“克隆”);
MethodAttributes methodIlAttributes=
(cloneMethod.Attributes&~MethodAttributes.Abstract)| MethodAttributes.Final;
MethodBuilder cloneMthdBldr=typeBuilder.DefineMethod(
“克隆”,methodIlAttributes,typeof(IMyClonable),newtype[]{});
ILGenerator ilgen=cloneMthdBldr.GetILGenerator();
LocalBuilder returnValue=ilgen.DeclareLocal(typeBuilder.AsType());
ilgen.Emit(操作码.Newobj,ctor);
ilgen.Emit(操作码.Stloc_S,返回值);
克隆属性(成分类型,ilgen);
ilgen.Emit(操作码.Ldloc_S);
ilgen.Emit(操作码Ret);
typeBuilder.DefineMethodOverride(cloneMthdBldr,cloneMethod);
}
专用静态无效克隆属性(类型componentType,ilgen生成器ilgen)
{
PropertyInfo[]allProperties=GetPublicProperties(componentType);
foreach(所有属性中的PropertyInfo propInfo)
{
ilgen.Emit(操作码Ldarg_0);
ilgen.Emit(opcode.Ldfld,builders[propInfo]);
ilgen.Emit(opcode.Stfld,builders[propInfo]);
ilgen.Emit(操作码Ldloc_0);
}
}

当我尝试调用Clone()方法时,得到System.InvalidProgrameException。即使我对方法CloneProperties()的调用进行注释。我做错了什么?我明白了!以下是工作代码:

private static void MakeCloneMethod(Type componentType,TypeBuilder-TypeBuilder)
{
键入thisType=typeBuilder.AsType();
类型itype=typeof(IMyClonable);
MethodInfo cloneMthd=itype.GetMethod(“克隆”);
MethodBuilder cloneMthdBldr=typeBuilder.DefineMethod(
cloneMthd.Name,cloneMthd.Attributes&~MethodAttributes.Abstract,
itype,新类型[]{});
typeBuilder.DefineMethodOverride(cloneMthdBldr,cloneMthd);
ILGenerator gen=cloneMthdBldr.GetILGenerator();
LocalBuilder returnValue=gen.DeclareLocal(此类型);
gen.Emit(opcode.Newobj,typeBuilder.DefineDefaultConstructor(MethodAttributes.Public));
gen.Emit(操作码.Stloc_S,返回值);
PropertyInfo[]allProperties=GetPublicProperties(componentType);
foreach(所有属性中的PropertyInfo propInfo)
{
gen.Emit(操作码.Ldloc_S,返回值);
gen.Emit(操作码Ldarg_0);
gen.Emit(操作码.Ldfld,生成器[propInfo]);
gen.Emit(opcode.Stfld,builders[propInfo]);
}
gen.Emit(操作码.Ldloc_S,返回值);
gen.Emit(操作码Ret);
}

谢谢你

我明白了!以下是工作代码:

private static void MakeCloneMethod(Type componentType,TypeBuilder-TypeBuilder)
{
键入thisType=typeBuilder.AsType();
类型itype=typeof(IMyClonable);
MethodInfo cloneMthd=itype.GetMethod(“克隆”);
MethodBuilder cloneMthdBldr=typeBuilder.DefineMethod(
cloneMthd.Name,cloneMthd.Attributes&~MethodAttributes.Abstract,
itype,新类型[]{});
typeBuilder.DefineMethodOverride(cloneMthdBldr,cloneMthd);
ILGenerator gen=cloneMthdBldr.GetILGenerator();
LocalBuilder returnValue=gen.DeclareLocal(此类型);
gen.Emit(opcode.Newobj,typeBuilder.DefineDefaultConstructor(MethodAttributes.Public));
gen.Emit(操作码.Stloc_S,返回值);
PropertyInfo[]allProperties=GetPublicProperties(componentType);
foreach(所有属性中的PropertyInfo propInfo)
{
gen.Emit(操作码.Ldloc_S,返回值);
gen.Emit(操作码Ldarg_0);
gen.Emit(操作码.Ldfld,生成器[propInfo]);
gen.Emit(opcode.Stfld,builders[propInfo]);
}
gen.Emit(操作码.Ldloc_S,返回值);
gen.Emit(操作码Ret);
}

谢谢你

您是否尝试将程序集保存到磁盘并在其上运行peverify?to svick:多亏了您,我发现了我的错误!您是否尝试将程序集保存到磁盘并在其上运行peverify?to svick:多亏了您,我发现了我的错误!