下面的代码编译在两个VS2013和VS2015,各种版本的.NET Framework 2.0至4.6.1时引发一个TypeLoadException,但是当被执行时将引发System.TypeLoadException:非常简单的代码编译,但执行
namespace Test
{
struct Foo<T>
{
}
struct Bar<U>
{
Foo<Bar<U>> foo;
}
class Program
{
static void Main(string[] args)
{
var x = new Bar<int>();
}
}
}
但是,如果Foo或Bar从结构更改为类,它将运行。我试图理解为什么这不适用于两个结构。为什么这个代码失败?
异常消息是:
"System.TypeLoadException occurred Message: A first chance exception of type 'System.TypeLoadException' occurred in mscorlib.dll Additional information: Could not load type 'Test.Bar`1' from assembly 'scratch, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'."
的[在结构VS类使用泛型]可能的复制(HTTP://计算器.com/questions/13731798/generics-used-in-struct-vs-class) – MethodMan
TypeLoadException的消息是什么? –
异常消息是:“发生了System.TypeLoadException异常 消息:mscorlib.dll中发生了类型'System.TypeLoadException'的第一次机会异常 其他信息:无法从程序集'scratch'加载类型'Test.Bar'1, Version = 1.0.0.0,Culture = neutral,PublicKeyToken = null'。“ –