Questions tagged «typeinitializer»

2
为什么要找到类型的初始化程序会引发NullReferenceException?
这让我难过。我正在尝试优化Noda Time的一些测试,其中我们进行了一些类型初始化程序检查。我以为在将所有内容加载到new之前,先要确定类型是否具有类型初始值设定项(静态构造函数或带有初始值设定项的静态变量)AppDomain。令我惊讶的是,NullReferenceException尽管我的代码中没有空值,但对此进行了一次小小的测试。仅当编译时没有调试信息时,它才会引发异常。 这是一个简短但完整的程序来演示该问题: using System; class Test { static Test() {} static void Main() { var cctor = typeof(Test).TypeInitializer; Console.WriteLine("Got initializer? {0}", cctor != null); } } 以及编译和输出记录: c:\Users\Jon\Test>csc Test.cs Microsoft (R) Visual C# Compiler version 4.0.30319.17626 for Microsoft (R) .NET Framework 4.5 Copyright (C) Microsoft Corporation. All rights reserved. …
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.