- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我目前正在开发 Moq 框架的扩展,以模拟非虚拟方法的实现。我目前已经通过获取原始方法的方法句柄并将其与用户定义的 Func 的指针交换来完成此工作。
我仍然遇到的一个问题是,当我在 Moq 内部代码(在使用泛型的类中)创建 Func 时,我遇到了 RuntimeHelpers.PrepareMethod 的问题。 (在执行指针交换之前需要准备好 Func)。
当我在普通类(例如程序)中创建完全相同的 Func 时,一切正常。
进一步调查该问题可以追溯到调用类是否具有通用参数。
抛出异常:
An unhandled exception of type 'System.ArgumentException' occurred in mscorlib.dll
Additional information: The given generic instantiation was invalid.
我已在以下代码块中隔离了问题:
class Program
{
static void Main(string[] args)
{
new WithoutGeneric().GoExecute();
new WithGeneric<string>().GoExecute();
}
}
public class WithoutGeneric
{
public void GoExecute()
{
//Works fine
StaticMethods.PrepareThisFunc(() => "Test");
}
}
public class WithGeneric<T>
{
public void GoExecute()
{
//Breaks
StaticMethods.PrepareThisFunc(() => "Test");
}
}
public static class StaticMethods
{
public static void PrepareThisFunc(Func<string> theFunc)
{
RuntimeHelpers.PrepareMethod(theFunc.Method.MethodHandle);
}
}
我也查看了当前的开源 CoreCLR 代码,但未能找出问题所在。
CoreCLR: https://github.com/dotnet/coreclr/blob/master/src/vm/reflectioninvocation.cpp
该行抛出异常:2435、2444、2447
有人知道如何解决此异常吗?
最佳答案
CLR 不知道 WithGeneric
类中的类型参数,它需要知道该类型参数才能创建调用图。
这样做可以解决问题:
class Program
{
static void Main(string[] args)
{
new WithoutGeneric().GoExecute();
new WithGeneric<string>().GoExecute();
}
}
public class WithoutGeneric
{
public void GoExecute()
{
//Works fine
StaticMethods.PrepareThisFunc1(() => "Test");
}
}
public class WithGeneric<T>
{
public void GoExecute()
{
//Works fine
StaticMethods.PrepareThisFunc2(() => "Test");
}
}
public static class StaticMethods
{
public static void PrepareThisFunc1(Func<string> theFunc)
{
RuntimeHelpers.PrepareMethod(theFunc.Method.MethodHandle);
}
public static void PrepareThisFunc2(Func<string> theFunc)
{
RuntimeHelpers.PrepareMethod(theFunc.Method.MethodHandle, new[] { typeof(string).TypeHandle });
}
}
关于c# - 使用在 Generic 类中创建的 Func<string> 调用时 RuntimeHelpers.PrepareMethod 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31754418/
我需要让我的应用程序中的一些代码被预编译。该场景是在 Windows Phone 下运行的 XNA 游戏。当爆炸第一次发生时,有一个明显的停顿,同时实现爆炸物理建模的方法是 JIT 的。我知道这一点,
关闭。这个问题是opinion-based .它目前不接受答案。 想要改进这个问题? 更新问题,以便 editing this post 可以用事实和引用来回答它. 关闭 8 年前。 Improve
我正在探索使用 RuntimeHelpers.PrepareMethod() 来减少具有大量 UI 库的瘦客户端应用程序的启动时间 我创建了一个 JIT 帮助程序类以在后台线程上运行并遍历类型或程序集
我目前正在开发 Moq 框架的扩展,以模拟非虚拟方法的实现。我目前已经通过获取原始方法的方法句柄并将其与用户定义的 Func 的指针交换来完成此工作。 我仍然遇到的一个问题是,当我在 Moq 内部代码
我是一名优秀的程序员,十分优秀!