다음을 통해 공유


TypeLoadException 생성자

정의

TypeLoadException 클래스의 새 인스턴스를 초기화합니다.

오버로드

Name Description
TypeLoadException()

TypeLoadException 클래스의 새 인스턴스를 초기화합니다.

TypeLoadException(String)

지정된 오류 메시지를 사용하여 클래스의 TypeLoadException 새 인스턴스를 초기화합니다.

TypeLoadException(SerializationInfo, StreamingContext)
사용되지 않음.

직렬화된 데이터를 사용하여 클래스의 새 인스턴스를 TypeLoadException 초기화합니다.

TypeLoadException(String, Exception)

지정된 오류 메시지와 이 예외의 TypeLoadException 원인인 내부 예외에 대한 참조를 사용하여 클래스의 새 인스턴스를 초기화합니다.

TypeLoadException()

Source:
TypeLoadException.cs
Source:
TypeLoadException.cs
Source:
TypeLoadException.cs
Source:
TypeLoadException.cs
Source:
TypeLoadException.cs

TypeLoadException 클래스의 새 인스턴스를 초기화합니다.

public:
 TypeLoadException();
public TypeLoadException();
Public Sub New ()

설명

이 생성자는 새 인스턴스의 속성을 "형식을 로드하는 동안 오류가 발생했습니다."와 같은 오류를 설명하는 시스템 제공 메시지로 초기화 Message 합니다. 이 메시지는 현재 시스템 문화권을 고려합니다.

다음 표에서는 인스턴스 TypeLoadException에 대한 초기 속성 값을 보여 줍니다.

재산 가치
InnerException Null 참조(Nothing Visual Basic의 경우)입니다.
Message 지역화된 오류 메시지 문자열입니다.

적용 대상

TypeLoadException(String)

Source:
TypeLoadException.cs
Source:
TypeLoadException.cs
Source:
TypeLoadException.cs
Source:
TypeLoadException.cs
Source:
TypeLoadException.cs

지정된 오류 메시지를 사용하여 클래스의 TypeLoadException 새 인스턴스를 초기화합니다.

public:
 TypeLoadException(System::String ^ message);
public TypeLoadException(string message);
public TypeLoadException(string? message);
new TypeLoadException : string -> TypeLoadException
Public Sub New (message As String)

매개 변수

message
String

오류를 설명하는 메시지입니다.

예제

다음 코드 예제에서는 생성자를 보여 줍니다 TypeLoadException(String) . 사용자 지정 메시지를 사용하여 생성 TypeLoadException 하고 콘솔에 오류 메시지를 표시하는 메서드를 포함합니다.

using System;

public class Example
{
   public static void Main()
   {
      try {
         // Call a method that throws an exception.
         TypeLoadExceptionDemoClass.GenerateException();
      }
      catch (TypeLoadException e) {
         Console.WriteLine("TypeLoadException:\n   {0}", e.Message);
      }
   }
}

class TypeLoadExceptionDemoClass
{
   public static bool GenerateException()
   {
      // Throw a TypeLoadException with a custom defined message.
      throw new TypeLoadException("This is a custom TypeLoadException error message.");
   }
}
// The example displays the following output:
//       TypeLoadException:
//          This is a custom TypeLoadException error message.
Public Class Example
   Public Shared Sub Main()
      Try
         ' Call a method that throws an exception.
         TypeLoadExceptionDemoClass.GenerateException()
      Catch e As TypeLoadException
         Console.WriteLine("TypeLoadException:{0}   {1}", vbCrLf, e.Message)
      End Try
   End Sub 
End Class 

Class TypeLoadExceptionDemoClass
   Public Shared Function GenerateException() As Boolean
      ' Throw a TypeLoadException with a custom message.
      Throw New TypeLoadException("This is a custom TypeLoadException error message.")
   End Function 
End Class 
' The example displays the following output:
'       TypeLoadException:
'          This is a custom TypeLoadException error message.

설명

매개 변수의 message 내용은 사용자가 이해할 수 있어야 합니다. 이 문자열이 현재 시스템 문화권에 맞게 지역화되었는지 확인하려면 이 생성자의 호출자가 필요합니다.

다음 표에서는 인스턴스 TypeLoadException에 대한 초기 속성 값을 보여 줍니다.

재산 가치
InnerException Null 참조(Nothing Visual Basic의 경우)입니다.
Message 오류 메시지 문자열입니다.

적용 대상

TypeLoadException(SerializationInfo, StreamingContext)

Source:
TypeLoadException.cs
Source:
TypeLoadException.cs
Source:
TypeLoadException.cs
Source:
TypeLoadException.cs
Source:
TypeLoadException.cs

주의

This API supports obsolete formatter-based serialization. It should not be called or extended by application code.

직렬화된 데이터를 사용하여 클래스의 새 인스턴스를 TypeLoadException 초기화합니다.

protected:
 TypeLoadException(System::Runtime::Serialization::SerializationInfo ^ info, System::Runtime::Serialization::StreamingContext context);
[System.Obsolete("This API supports obsolete formatter-based serialization. It should not be called or extended by application code.", DiagnosticId="SYSLIB0051", UrlFormat="https://aka.ms/dotnet-warnings/{0}")]
protected TypeLoadException(System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context);
protected TypeLoadException(System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context);
[<System.Obsolete("This API supports obsolete formatter-based serialization. It should not be called or extended by application code.", DiagnosticId="SYSLIB0051", UrlFormat="https://aka.ms/dotnet-warnings/{0}")>]
new TypeLoadException : System.Runtime.Serialization.SerializationInfo * System.Runtime.Serialization.StreamingContext -> TypeLoadException
new TypeLoadException : System.Runtime.Serialization.SerializationInfo * System.Runtime.Serialization.StreamingContext -> TypeLoadException
Protected Sub New (info As SerializationInfo, context As StreamingContext)

매개 변수

info
SerializationInfo

serialize된 개체 데이터를 보유하는 개체입니다.

context
StreamingContext

원본 또는 대상에 대한 컨텍스트 정보입니다.

특성

예외

개체는 info .입니다 null.

예제

다음 예제에서는 예외를 생성하고 예외 데이터를 파일로 serialize한 다음 예외를 다시 구성합니다. 이 코드 예제를 실행하려면 정규화된 어셈블리 이름을 제공해야 합니다. 정규화된 어셈블리 이름을 가져오는 방법에 대한 자세한 내용은 어셈블리 이름을 참조하세요.


using System;
using System.Reflection;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Soap;
using System.IO;

class GetObjectDataDemo
{
   public static void Main()
   {
      // Get a reference to the assembly mscorlib.dll, which is always
      // loaded. (System.String is defined in mscorlib.)
      Assembly mscorlib = typeof(string).Assembly;

      try
      {
         Console.WriteLine ("Attempting to load a type not present in the assembly 'mscorlib'");
         // This loading of invalid type raises a TypeLoadException
         Type myType = mscorlib.GetType("System.NonExistentType", true);
      }
      catch (TypeLoadException)
      {
         // Serialize the exception to disk and reconstitute it.
         System.DateTime ErrorDatetime = DateTime.Now;
         Console.WriteLine("A TypeLoadException has been raised.");

         // Create MyTypeLoadException instance with current time.
         MyTypeLoadException myException = new MyTypeLoadException(ErrorDatetime);
         IFormatter myFormatter = new SoapFormatter();
         Stream myFileStream = new FileStream("typeload.xml", FileMode.Create, FileAccess.Write, FileShare.None);
         Console.WriteLine("Serializing the TypeLoadException with DateTime as " + ErrorDatetime);

         // Serialize the MyTypeLoadException instance to a file.
         myFormatter.Serialize(myFileStream, myException);
         myFileStream.Close();

         Console.WriteLine("Deserializing the Exception.");
         myFileStream = new FileStream("typeload.xml", FileMode.Open, FileAccess.Read, FileShare.None);

         // Deserialize and reconstitute the instance from file.
         myException = (MyTypeLoadException) myFormatter.Deserialize(myFileStream);
         myFileStream.Close();
         Console.WriteLine("Deserialized exception has ErrorDateTime = " + myException.ErrorDateTime);
      }
   }
}

// This class overrides the GetObjectData method and initializes
// its data with current time.

[Serializable]
public class MyTypeLoadException : TypeLoadException
{
   private System.DateTime _errorDateTime = DateTime.Now;
   public DateTime ErrorDateTime { get { return _errorDateTime; }}

   public MyTypeLoadException(DateTime myDateTime)
   {
      _errorDateTime = myDateTime;
   }

   protected MyTypeLoadException(SerializationInfo sInfo, StreamingContext sContext)
       : base(sInfo, sContext)
   {
      // Reconstitute the deserialized information into the instance.
      _errorDateTime = sInfo.GetDateTime("ErrorDate");
   }

   public override void GetObjectData(SerializationInfo sInfo, StreamingContext sContext)
   {
      base.GetObjectData(sInfo, sContext);
      // Add a value to the Serialization information.
      sInfo.AddValue("ErrorDate", ErrorDateTime);
   }
}
Imports System.Reflection
Imports System.Runtime.Serialization
Imports System.Runtime.Serialization.Formatters.Soap
Imports System.Security.Permissions
Imports System.IO

Class GetObjectDataDemo

   Public Shared Sub Main()
      ' Get a reference to the assembly mscorlib.dll, which is always
      ' loaded. (System.String is defined in mscorlib.)
      Dim tString As Type = GetType(String)
      Dim mscorlib As [Assembly] = tString.Assembly

      Try
         Console.WriteLine("Attempting to load a type not present in the assembly 'mscorlib'")
         ' This loading of invalid type raises a TypeLoadException
         Dim myType As Type = mscorlib.GetType("System.NonExistentType", True)
      Catch
         ' Serialize the exception to disk and reconstitute it.
         Dim ErrorDatetime as System.DateTime = DateTime.Now
         Console.WriteLine("A TypeLoadException has been raised.")

         ' Create MyTypeLoadException instance with current time.
         Dim myException As new MyTypeLoadException(ErrorDatetime)
         Dim myFormatter as IFormatter  = new SoapFormatter()
         Dim myFileStream as Stream 
         myFileStream = New FileStream("typeload.xml", FileMode.Create, FileAccess.Write, FileShare.None)
         Console.WriteLine("Serializing the TypeLoadException with DateTime as " _
             & ErrorDatetime.ToString())

         ' Serialize the MyTypeLoadException instance to a file.
         myFormatter.Serialize(myFileStream, myException)
         myFileStream.Close()

         Console.WriteLine("Deserializing the Exception.")
         myFileStream = New FileStream("typeload.xml", FileMode.Open, FileAccess.Read, FileShare.None)

         ' Deserialize and reconstitute the instance from file.
         myException = CType(myFormatter.Deserialize(myFileStream), MyTypeLoadException)
         myFileStream.Close()
         Console.WriteLine("Deserialized exception has ErrorDateTime = " + myException.ErrorDateTime.ToString())
      End Try
   End Sub
End Class

' This class overrides the GetObjectData method and initializes
' its data with current time. 
<Serializable()> _
Public Class MyTypeLoadException
   Inherits TypeLoadException

   Private _errorDateTime As System.DateTime = DateTime.Now
   Public ReadOnly Property ErrorDateTime As DateTime
      Get
         Return _errorDateTime
      End Get
   End Property

   Public Sub New(myDateTime As DateTime)
      _errorDateTime = myDateTime
   End Sub

   Protected Sub New(sInfo As SerializationInfo, sContext As StreamingContext)
      MyBase.New(sInfo, sContext)
      ' Reconstitute the deserialized information into the instance.
      _errorDateTime = sInfo.GetDateTime("ErrorDate")
   End Sub

   ' GetObjectData overrides must always have a demand for SerializationFormatter.
   <SecurityPermissionAttribute(SecurityAction.Demand, SerializationFormatter:=true)> _
   Public Overrides Sub GetObjectData(sInfo As SerializationInfo, sContext As StreamingContext)
      MyBase.GetObjectData(sInfo, sContext)
      ' Add a value to the Serialization information.
      sInfo.AddValue("ErrorDate", ErrorDateTime)
   End Sub

End Class

설명

이 생성자는 역직렬화 중에 호출되어 스트림을 통해 전송되는 예외 개체를 다시 구성합니다. 자세한 내용은 XML 및 SOAP Serialization을 참조하세요.

추가 정보

적용 대상

TypeLoadException(String, Exception)

Source:
TypeLoadException.cs
Source:
TypeLoadException.cs
Source:
TypeLoadException.cs
Source:
TypeLoadException.cs
Source:
TypeLoadException.cs

지정된 오류 메시지와 이 예외의 TypeLoadException 원인인 내부 예외에 대한 참조를 사용하여 클래스의 새 인스턴스를 초기화합니다.

public:
 TypeLoadException(System::String ^ message, Exception ^ inner);
public TypeLoadException(string message, Exception inner);
public TypeLoadException(string? message, Exception? inner);
new TypeLoadException : string * Exception -> TypeLoadException
Public Sub New (message As String, inner As Exception)

매개 변수

message
String

예외의 이유를 설명하는 오류 메시지입니다.

inner
Exception

현재 예외의 원인인 예외입니다. 매개 변수가 inner 아닌 null경우 내부 예외를 catch 처리하는 블록에서 현재 예외가 발생합니다.

예제

다음 코드 예제에서는 생성자를 보여 줍니다 TypeLoadException(String, Exception) . 이 메서드는 해당 예외를 TypeLoadException생성하고, 해당 예외를 catch하고, 원본 TypeLoadException 을 내부 예외로 포함하여 사용자 지정 메시지를 사용하여 새 TypeLoadException 메서드를 throw하는 메서드를 포함합니다.

using System;
using System.Runtime.InteropServices;

public class TypeLoadException_Constructor3
{
   public static void Main()
   {
      Console.WriteLine("Calling a method in a non-existent DLL which triggers a TypeLoadException.");
      try
      {
         TypeLoadExceptionDemoClass3.GenerateException();
      }
      catch (TypeLoadException e)
      {
         Console.WriteLine ("TypeLoadException: \n\tError Message = " + e.Message);
         Console.WriteLine ("TypeLoadException: \n\tInnerException Message = " + e.InnerException.Message );
      }
      catch (Exception e)
      {
         Console.WriteLine ("Exception: \n\tError Message = " + e.Message);
      }
   }
}

class TypeLoadExceptionDemoClass3
{
   // A call to this method will raise a TypeLoadException.
   [DllImport("NonExistentDLL.DLL", EntryPoint="MethodNotExists")]
   public static extern void NonExistentMethod();

   public static void GenerateException()
   {
      try
      {
         NonExistentMethod();
      }
      catch (TypeLoadException e)
      {
         // Rethrow exception with the exception as inner exception
         throw new TypeLoadException("This exception was raised due to a call to an invalid method.", e);
      }
   }
}
Imports System.Runtime.InteropServices

Public Class TypeLoadException_Constructor3
   Public Shared Sub Main()
      Console.WriteLine("Calling a method in a non-existent DLL which triggers a TypeLoadException.")
      Try
         TypeLoadExceptionDemoClass.GenerateException()
      Catch e As TypeLoadException
         Console.WriteLine(("TypeLoadException: " + ControlChars.Cr + ControlChars.Tab + "Error Message = " + e.Message))
         Console.WriteLine(("TypeLoadException: " + ControlChars.Cr + ControlChars.Tab + "InnerException Message = " + e.InnerException.Message))
      Catch e As Exception
         Console.WriteLine(("Exception: " + ControlChars.Cr + ControlChars.Tab + "Error Message = " + e.Message))
      End Try
   End Sub
End Class

Class TypeLoadExceptionDemoClass
   ' A call to this method will raise a TypeLoadException.
   Public Declare Sub NonExistentMethod Lib "NonExistentDLL.DLL" Alias "MethodNotExists" ()

   Public Shared Sub GenerateException()
      Try
         NonExistentMethod()
      Catch e As TypeLoadException
         ' Rethrow exception with the exception as inner exception
         Throw New TypeLoadException("This exception was raised due to a call to an invalid method.", e)
      End Try
   End Sub
End Class

설명

이전 예외의 직접적인 결과로 throw되는 예외에는 속성의 이전 예외에 대한 참조가 InnerException 포함될 수 있습니다. 속성 생성 InnerException 자에 전달 되는 동일한 값을 반환 하거나 null 속성 생성자에 내부 예외 값을 제공 하지 않는 경우 InnerException .

다음 표에서는 인스턴스 TypeLoadException에 대한 초기 속성 값을 보여 줍니다.

재산 가치
InnerException 내부 예외 참조입니다.
Message 오류 메시지 문자열입니다.

추가 정보

적용 대상