为什么这段代码会抛出"System.Security.VerificationException:操作会破坏运行时的稳定“?
MethodInfo mi = typeof(TypedReference).GetMethod("InternalMakeTypedReference", BindingFlags.NonPublic | BindingFlags.Static);
ParameterExpression p1 = Expression.Parameter(typeof(IntPtr));
ParameterExpression p2 = Expression.Parameter(typeof(object));
ParameterExpression p3 = Expression.Parameter(typeof(IntPtr[]));
ParameterExpression p4 = Expression.Parameter(typeof(Type));
Expression exp = Expression.Call(mi, Expression.Convert(p1, typeof(void*)), p2, p3, Expression.Convert(p4, Types.RuntimeType));
var m = Expression.Lambda<Action<IntPtr,object,IntPtr[],Type>>(exp, p1, p2, p3, p4).Compile();
m(IntPtr.Zero,null,null,null);异常不会因为错误的论点而引发。
发布于 2014-12-13 22:47:32
据我所知,您不能构建包含不安全代码的表达式树。不安全的代码意味着不能验证类型/内存的安全性,但是根据此错误描述,只有在代码是可验证的情况下才能编译表达式。
在表达式树中传递IntPtr应该很好,但是如果参数是void*,那就没有帮助了。
一种可行的方法可能是使用Reflection.Emit生成直接访问IL的方法。默认情况下,它不能调用私有方法,但是下面是一个解决这个问题的答案:https://stackoverflow.com/a/1778446/1659828
无论如何,在您的情况下,我不知道您想要完成什么,但是尝试使用非CLS兼容API的实现细节似乎有更好的方法。
https://stackoverflow.com/questions/27463129
复制相似问题