【发布时间】:2010-07-05 23:21:46
【问题描述】:
正在使用 this solution 将匿名类型转换为使用反射.emit 的字典。在我从 3.5 更改为 .Net 4.0 之前工作正常。
现在,我收到“System.Security.VerificationException:操作可能会破坏运行时”。错误。
将匿名加载的动态方法转换为托管在动态程序集中的方法,保存它,然后在其上运行 peverify.exe 以找出问题所在。
得到:[IL]:错误:[DynamicAssemblyExample.dll:MyDynamicType::MyMethod][offs et 0x0000000D][found ref ('this' ptr) 'MyDynamicType'][expected ref 'f__AnonymousType1`3[System.String,System.Int32,System.Byte]'] stac 上的意外类型 ķ。 [IL]:错误:[DynamicAssemblyExample.dll:MyDynamicType::MyMethod][offs et 0x0000000D] 方法不可见。 2 验证 DynamicAssemblyExample.dll 的错误
代码:
foreach (PropertyInfo property in itemType.GetProperties(attributes).Where(info => info.CanRead))
{
// load Dictionary (prepare for call later)
methIL.Emit(OpCodes.Ldloc_0);
// load key, i.e. name of the property
methIL.Emit(OpCodes.Ldstr, property.Name);
// load value of property to stack
methIL.Emit(OpCodes.Ldarg_0);
methIL.EmitCall(OpCodes.Callvirt, property.GetGetMethod(), null);
// perform boxing if necessary
if (property.PropertyType.IsValueType)
{
methIL.Emit(OpCodes.Box, property.PropertyType);
}
// stack at this point
// 1. string or null (value)
// 2. string (key)
// 3. dictionary
// ready to call dict.Add(key, value)
methIL.EmitCall(OpCodes.Callvirt, addMethod, null);
}
有没有办法取消指向实际属性的指针?还是我必须以某种方式投射它?有什么指点吗?
问候!
【问题讨论】:
标签: c#-4.0 reflection.emit opcodes