什么是System.TypeInitializationException
作为类初始值设定项引发的异常的包装器而引发的异常。
- 继承
-
ObjectExceptionSystemExceptionTypeInitializationException
详细说明
TypeInitializationException属性包含基础异常。
相反, 应调查并消除异常的原因。
引发的原因一般情况如下:
静态构造函数和 System.typeinitializationexception 异常
-
有关静态构造函数的详细信息, 请参阅静态构造函数。
TypeInitializationException异常的一些更常见的原因如下:
-
静态构造函数中出现未经处理的异常
TypeInitializationException异常中, 并且无法实例化该类型。
如果存在以下情况, 则类型中存在静态构造函数:
-
它已显式定义为类型的成员。
-
例如, 当C#和 VB 编译器编译以下示例时, 它们将为类似于以下内容的静态构造函数生成 IL:
-
.method private specialname rtspecialname static void .cctor() cil managed { // Code size 12 (0xc) .maxstack 8 IL_0000: ldc.i4.3 IL_0001: newobj instance void TestClass::.ctor(int32) IL_0006: stsfld class TestClass Example::test IL_000b: ret } // end of method Example::.cctorTypeInitializationException异常中。
C# -
-
using System; public class Example { private static TestClass test = new TestClass(3); public static void Main() { Example ex = new Example(); Console.WriteLine(test.Value); } } public class TestClass { public readonly int Value; public TestClass(int value) { if (value < 0 || value > 1) throw new ArgumentOutOfRangeException(); Value = value; } } // The example displays the following output: // Unhandled Exception: System.TypeInitializationException: // The type initializer for 'Example' threw an exception. ---> // System.ArgumentOutOfRangeException: Specified argument was out of the range of valid values. // at TestClass..ctor(Int32 value) // at Example..cctor() // --- End of inner exception stack trace --- // at Example.Main()InnerException的信息。
-
缺少程序集或数据文件
例如, 可以使用以下命令行语法将下面的示例编译为名为 Missing1a 的程序集:
C#
csc -t:library Missing1a.cs
using System;
public class InfoModule
{
private DateTime firstUse;
private int ctr = 0;
public InfoModule(DateTime dat)
{
firstUse = dat;
}
public int Increment()
{
return ++ctr;
}
public DateTime GetInitializationTime()
{
return firstUse;
}
}
然后, 可以通过包含对 Missing1a 的引用, 将以下示例编译到名为 Missing1 的可执行文件:
csc Missing1.cs /r:Missing1a.dll
FileNotFoundException引发的, 因为运行时找不到依赖程序集。
using System;
public class Example
{
public static void Main()
{
Person p = new Person("John", "Doe");
Console.WriteLine(p);
}
}
public class Person
{
static InfoModule infoModule;
String fName;
String mName;
String lName;
static Person()
{
infoModule = new InfoModule(DateTime.UtcNow);
}
public Person(String fName, String lName)
{
this.fName = fName;
this.lName = lName;
infoModule.Increment();
}
public override String ToString()
{
return String.Format("{0} {1}", fName, lName);
}
}
// The example displays the following output if missing1a.dll is renamed or removed:
// Unhandled Exception: System.TypeInitializationException:
// The type initializer for 'Person' threw an exception. --->
// System.IO.FileNotFoundException: Could not load file or assembly
// 'Missing1a, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null'
// or one of its dependencies. The system cannot find the file specified.
// at Person..cctor()
// --- End of inner exception stack trace ---
// at Person..ctor(String fName, String lName)
// at Example.Main()
备注
如果静态构造函数尝试打开数据文件 (如配置文件、XML 文件或包含序列化数据的文件), 则也可能会引发异常。
正则表达式匹配超时值
TypeInitializationException在异常中。
日历和文化数据
以下 calendar 类构造函数可能会引发此异常:
-
JapaneseCalendar类的无参数构造函数。
-
KoreanCalendar类的无参数构造函数。
-
TaiwanCalendar类的无参数构造函数。
由于这些区域性的区域性数据应在所有系统上都可用, 因此, 您几乎不会遇到此异常。
HRESULT
TypeInitializationException使用 COR_E_TYPEINITIALIZATION 值为0x80131534 的 HRESULT。