本文整理汇总了C#中System.TypeInitializationException类的典型用法代码示例。如果您正苦于以下问题:C# TypeInitializationException类的具体用法?C# TypeInitializationException怎么用?C# TypeInitializationException使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
TypeInitializationException类属于System命名空间,在下文中一共展示了TypeInitializationException类的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Main
//引入命名空间
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;
}
}
输出:
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()
示例2: InfoModule
//引入命名空间
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;
}
}
示例3: Main
//引入命名空间
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()
示例4: Main
//引入命名空间
using System;
using System.Text.RegularExpressions;
public class Example
{
public static void Main()
{
AppDomain domain = AppDomain.CurrentDomain;
// Set a timeout interval of -2 seconds.
domain.SetData("REGEX_DEFAULT_MATCH_TIMEOUT", TimeSpan.FromSeconds(-2));
Regex rgx = new Regex("[aeiouy]");
Console.WriteLine("Regular expression pattern: {0}", rgx.ToString());
Console.WriteLine("Timeout interval for this regex: {0} seconds",
rgx.MatchTimeout.TotalSeconds);
}
}
输出:
Unhandled Exception: System.TypeInitializationException: The type initializer for 'System.Text.RegularExpressions.Regex' threw an exception. ---> System.ArgumentOutOfRangeException: Specified argument was out of the range of valid values. Parameter name: AppDomain data 'REGEX_DEFAULT_MATCH_TIMEOUT' contains an invalid value or object for specifying a default matching timeout for System.Text.RegularExpressions.Regex. at System.Text.RegularExpressions.Regex.InitDefaultMatchTimeout() at System.Text.RegularExpressions.Regex..cctor() --- End of inner exception stack trace --- at System.Text.RegularExpressions.Regex..ctor(String pattern) at Example.Main()