为什么使用Moq创建的模拟实例会抛出System.BadImageFormatException?

Jon*_*ney 5 debugging castle-dynamicproxy moq reflection.emit

这个问题可能与另一个问题有关,它肯定会导致System.BadImageFormatException.也许它是相同的东西,但暴露不同?

我有以下代码:

public interface IFoo<T> where T : class, new() {
  T FooMethod(object o);
}

public interface IFooRepo {
  F GetFoo<T, F>() where T : class, new() where F : IFoo<T>;
}
Run Code Online (Sandbox Code Playgroud)

然后我有一个测试,使用Moq模拟IFooRepo,如下所示:

var instance = new Mock<IFooRepo>().Object;
Run Code Online (Sandbox Code Playgroud)

上面的代码运行正常,除非在使用Visual Studio 2008调试测试时.当我跳过上面的行时,会通过Castle.DynamicProxy从System.Reflection.Emit抛出System.BadImageFormatException.难道这是类似的东西 Ayende Rahien贴?

现在的解决方法是为IFooRepo实现一个假,但我很好奇为什么会为这种情况生成一个坏图像并且有一个修复?System.Reflection.Emit是否有错误?或者我在自己的代码中遗漏了一些明显的东西?

编辑:为GetFoo()发布了错误的签名.将签名更正为GetFoo <T,F>(),正确地再现了问题.安装GDR后,此问题仍然存在.

编辑:似乎如果F上的约束包括类型参数T BadImageFormatException被引发.但是我把它更改为,where F : class, new()然后一切都按预期工作.