C# 4.0 在.Net 4.0中无法验证的动态方法代码(找到ref';this';指针…预期ref';lt;gt;f#u匿名类型1`)
用于使用reflection.emit将匿名类型转换为字典。在我从3.5改为.NET4.0之前,它工作得很好 现在,我得到了“System.Security.VerificationException:操作可能会破坏运行时的稳定性”错误 将匿名加载的动态方法转换为托管在动态程序集中的方法,保存该方法,然后在其上运行peverify.exe以找出错误 获取:[IL]:错误:[DynamicCasSemblyExample.dll:MyDynamicType::MyMethod][OFF et 0x0000000D][found ref('this'ptr)'MyDynamicType'][expected ref'f_uuanonymoustype1`3[System.String,System.Int32,System.Byte]]stac上的意外类型 K [IL]:错误:[DynamicCasSemblyExample.dll:MyDynamicType::MyMethod][offs et 0x0000000D]方法不可见。 验证DynamicCasSemblyExample.dll时出现2个错误 守则:C# 4.0 在.Net 4.0中无法验证的动态方法代码(找到ref';this';指针…预期ref';lt;gt;f#u匿名类型1`),c#-4.0,reflection.emit,opcodes,C# 4.0,Reflection.emit,Opcodes,用于使用reflection.emit将匿名类型转换为字典。在我从3.5改为.NET4.0之前,它工作得很好 现在,我得到了“System.Security.VerificationException:操作可能会破坏运行时的稳定性”错误 将匿名加载的动态方法转换为托管在动态程序集中的方法,保存该方法,然后在其上运行peverify.exe以找出错误 获取:[IL]:错误:[DynamicCasSemblyExample.dll:MyDynamicType::MyMethod][OFF et 0x
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);
}
有没有办法解除指针到实际属性的限制?还是我必须以某种方式投下它?有什么建议吗
问候 抱歉,各位犯了一个错误,因为实际的动态方法创建了一个作用于匿名(或非匿名)类型实例的委托类型,所以Ldarg_0代码正在查找此调试实现中不存在的内容 所以我把它改成了OpCodes.Ldnull
var attributes = BindingFlags.Instance | BindingFlags.Public | BindingFlags.FlattenHierarchy;
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.Ldnull);
//methIL.Emit(OpCodes.Castclass, itemType);
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);
}
但我仍然得到一个方法不可见的错误后,验证它。匿名类型属性的get方法是否通过反射不可见 只是一个建议,您是否尝试过重写发出IL的代码以实际写入字典-即no
Reflection.Emit
?我打赌生成的IL在某种程度上是不正确的,而不是访问匿名类型的代码。是的,我做了一个测试,在没有反射的情况下进行了测试,除了castclass操作码外,它看起来几乎一样。。。这给了我一个想法。耶,问题解决了。我只需要提出论点,它就行了。NET3.5运行时验证程序显然存在一些漏洞。谢谢你的回复。