当前位置: 首页>>代码示例>>C#>>正文


C# TypeInitializationException类代码示例

本文整理汇总了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;
   }
}
开发者ID:.NET开发者,项目名称:System,代码行数:24,代码来源:TypeInitializationException

输出:

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;
   }
}
开发者ID:.NET开发者,项目名称:System,代码行数:23,代码来源:TypeInitializationException

示例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()
开发者ID:.NET开发者,项目名称:System,代码行数:47,代码来源:TypeInitializationException

示例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);
   }
}
开发者ID:.NET开发者,项目名称:System,代码行数:18,代码来源:TypeInitializationException

输出:

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()


注:本文中的System.TypeInitializationException类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。