【发布时间】:2016-04-01 18:36:23
【问题描述】:
IL 新手...尝试为以下对象创建 IL:
Dest CreateInstance(Source src)
{
Dest d = new Dest();
d.Test = src.Test;
return d;
}
这是我目前所拥有的:
ConstructorInfo ctor = typeof(Dest).GetConstructors()[0];
DynamicMethod method = new DynamicMethod("CreateIntance", typeof(Dest),
new Type[] { typeof(Source) });
ILGenerator gen = method.GetILGenerator();
//gen.Emit(OpCodes.Ldarg_0);// source
gen.Emit(OpCodes.Newobj, ctor);// new Created
gen.Emit(OpCodes.Ret);
CreateCtor createdCtorDelegate;
createdCtorDelegate = (CreateCtor)method.CreateDelegate(typeof(CreateCtor));
这运行如上...但如果我取消注释掉 Ldarg_0,当我尝试调用委托时,我会收到“此操作可能会导致运行时不稳定”。
另外,我需要什么来复制 Test 成员?假设它是一个基本类型。
谢谢!
编辑:
Source 和 Dest 以及简单的 POCO。
public class Source
{
public string S1 { get; set; }
public string S2 { get; set; }
public int I1 { get; set; }
public int I2 { get; set; }
public string S3 { get; set; }
public string S4 { get; set; }
public string S5 { get; set; }
}
public class Dest
{
public string S1 { get; set; }
public string S2 { get; set; }
public int I1 { get; set; }
public int I2 { get; set; }
public string S3 { get; set; }
public string S4 { get; set; }
public string S5 { get; set; }
}
编辑 #2:现在,我有这个......仍然得到 destabalize 错误:
ConstructorInfo ctor = typeof(Dest).GetConstructors()[0];
DynamicMethod method = new DynamicMethod("CreateIntance", typeof(Dest),
new Type[] { typeof(Source) });
MethodInfo miSrc = tSource.GetProperty("S1").GetGetMethod();
MethodInfo miDest = tDest.GetProperty("S1").GetSetMethod();
ILGenerator gen = method.GetILGenerator();
gen.Emit(OpCodes.Newobj, ctor);// new Created
gen.Emit(OpCodes.Dup);
gen.Emit(OpCodes.Ldarg_1);// source
gen.Emit(OpCodes.Ldfld, miSrc);
gen.Emit(OpCodes.Stfld, miDest);
gen.Emit(OpCodes.Ret);
CreateCtor createdCtorDelegate;
createdCtorDelegate = (CreateCtor)method.CreateDelegate(typeof(CreateCtor));
Dest dd = createdCtorDelegate(s);
调用 createdCtorDelegate 时获取异常。
EDIT3:
ILSpy 显示:
.method public hidebysig static
class ConsoleApplication3.Dest Test (
class ConsoleApplication3.Source s
) cil managed
{
// Method begins at RVA 0x2148
// Code size 26 (0x1a)
.maxstack 2
.locals init (
[0] class ConsoleApplication3.Dest,
[1] class ConsoleApplication3.Dest
)
IL_0000: nop
IL_0001: newobj instance void ConsoleApplication3.Dest::.ctor()
IL_0006: stloc.0
IL_0007: ldloc.0
IL_0008: ldarg.0
IL_0009: callvirt instance string ConsoleApplication3.Source::get_S1()
IL_000e: callvirt instance void ConsoleApplication3.Dest::set_S1(string)
IL_0013: nop
IL_0014: ldloc.0
IL_0015: stloc.1
IL_0016: br.s IL_0018
IL_0018: ldloc.1
IL_0019: ret
} // end of method Program::Test
所以,我已将代码调整为:
ConstructorInfo ctor = typeof(Dest).GetConstructors()[0];
DynamicMethod method = new DynamicMethod("CreateIntance", typeof(Dest),
new Type[] { typeof(Source) });
MethodInfo miSrc = tSource.GetProperty("S1").GetGetMethod();
MethodInfo miDest = tDest.GetProperty("S1").GetSetMethod();
ILGenerator gen = method.GetILGenerator();
gen.Emit(OpCodes.Newobj, ctor);// new Created
gen.Emit(OpCodes.Stloc_0);
gen.Emit(OpCodes.Ldloc_0);
gen.Emit(OpCodes.Ldarg_0);
gen.Emit(OpCodes.Callvirt, miSrc);
gen.Emit(OpCodes.Callvirt, miDest);
gen.Emit(OpCodes.Ldloc_0);
gen.Emit(OpCodes.Stloc_1);
gen.Emit(OpCodes.Ldloc_1);
gen.Emit(OpCodes.Ret);
CreateCtor createdCtorDelegate;
createdCtorDelegate = (CreateCtor)method.CreateDelegate(typeof(CreateCtor));
仍然崩溃:(...
【问题讨论】:
-
你能指定“基本类型”吗?你的意思是原始类型或结构还是...?
-
鉴于表达式树的存在,您是否知道反射发射几乎已过时?
-
专业提示:将你的 IL 转储到一个文件并在其上运行 PEVerify。
标签: c# .net reflection cil emit