下面的代碼編譯在兩個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'。「 –