當前位置: 首頁>>代碼示例>>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;未經允許,請勿轉載。