DynamicMethod 代码在 .Net 4.0 中无法验证(找到引用“this”指针...预期引用“f__AnonymousType1”)
正在使用此解决方案 使用 Reflection.emit 将匿名类型转换为字典。工作正常,直到我从 3.5 更改为 .Net 4.0。
现在,我收到“System.Security.VerificationException:操作可能会破坏运行时的稳定性”。错误。
将匿名加载的动态方法转换为动态程序集中托管的方法,保存它,然后对其运行 peverify.exe 以找出问题所在。
得到:[IL]:错误:[DynamicAssemblyExample.dll:MyDynamicType::MyMethod][offs et 0x0000000D][找到 ref ('this' ptr) 'MyDynamicType'][预期 ref '<>f__AnonymousType1`3[System.String,System.Int32,System.Byte]'] stac 上的意外类型 k. [IL]:错误:[DynamicAssemblyExample.dll:MyDynamicType::MyMethod][offs et 0x0000000D] 方法不可见。 验证 DynamicAssemblyExample.dll 时出现 2 个错误
代码:
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);
}
有没有办法取消引用指向实际属性的指针?或者我必须以某种方式投射它?有什么指点吗?
问候!
Was using this solution to convert anonymous types to dictionaries using reflection.emit. Was working fine until I changed to .Net 4.0 from 3.5.
Now, I'm getting the "System.Security.VerificationException: Operation could destabilize the runtime." error.
Converted the anonymously loaded dynamic method to one hosted in a dynamic assembly, saved it, then ran peverify.exe on it to find out what was wrong.
Got: [IL]: Error: [DynamicAssemblyExample.dll : MyDynamicType::MyMethod][offs
et 0x0000000D][found ref ('this' ptr) 'MyDynamicType'][expected ref '<>f__AnonymousType1`3[System.String,System.Int32,System.Byte]'] Unexpected type on the stac
k.
[IL]: Error: [DynamicAssemblyExample.dll : MyDynamicType::MyMethod][offs
et 0x0000000D] Method is not visible.
2 Error(s) Verifying DynamicAssemblyExample.dll
The code:
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);
}
Is there a way to derefence the pointer to the actual property? Or do I have to cast it somehow? Any pointers?
Regards!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
抱歉,大家犯了一个错误,因为实际的动态方法创建了一个作用于匿名(或非匿名)类型实例的委托类型,所以 Ldarg_0 代码正在寻找此调试实现中不存在的东西。
所以我将其更改为 OpCodes.Ldnull。
但在将其固定后,我仍然收到方法不可见的错误。匿名类型属性的 get 方法是否通过反射不可见?
Sorry guys, made a mistake, since the actual dynamic method creates a delegate type that acts on the instance of the anonymous (or non-anonymous) type, the Ldarg_0 code is looking for a something that is not there in this debug implementation.
So I, changed it to OpCodes.Ldnull.
But I still get a method not visible error after peverifying it. Is it that get methods for properties of anonymous types are not visible via reflection?
只是一个建议,您是否尝试重写发出 IL 的代码以实际写入字典 - 即没有
Reflection.Emit
?我敢打赌,生成的 IL 在某种程度上是不正确的,而不是访问匿名类型的代码。Just a suggestion, have you tried to rewrite the code that emits IL to actually write to the dictionary - i.e. no
Reflection.Emit
? My bet is that the generated IL is not proper in some way, not the code that accesses the anonymous type.