Udostępnij za pośrednictwem


TypeLoadException Konstruktory

Definicja

Inicjuje nowe wystąpienie klasy TypeLoadException.

Przeciążenia

Nazwa Opis
TypeLoadException()

Inicjuje nowe wystąpienie klasy TypeLoadException.

TypeLoadException(String)

Inicjuje nowe wystąpienie TypeLoadException klasy z określonym komunikatem o błędzie.

TypeLoadException(SerializationInfo, StreamingContext)
Przestarzałe.

Inicjuje TypeLoadException nowe wystąpienie klasy z serializowanymi danymi.

TypeLoadException(String, Exception)

Inicjuje nowe wystąpienie TypeLoadException klasy z określonym komunikatem o błędzie i odwołaniem do wyjątku wewnętrznego, który jest przyczyną tego wyjątku.

TypeLoadException()

Źródło:
TypeLoadException.cs
Źródło:
TypeLoadException.cs
Źródło:
TypeLoadException.cs
Źródło:
TypeLoadException.cs
Źródło:
TypeLoadException.cs

Inicjuje nowe wystąpienie klasy TypeLoadException.

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

Uwagi

Ten konstruktor inicjuje Message właściwość nowego wystąpienia do komunikatu dostarczonego przez system, który opisuje błąd, taki jak "Wystąpił błąd podczas ładowania typu". Ten komunikat uwzględnia bieżącą kulturę systemu.

W poniższej tabeli przedstawiono początkowe wartości właściwości dla wystąpienia TypeLoadExceptionklasy .

Majątek Wartość
InnerException Odwołanie o wartości null (Nothing w Visual Basic).
Message Zlokalizowany ciąg komunikatu o błędzie.

Dotyczy

TypeLoadException(String)

Źródło:
TypeLoadException.cs
Źródło:
TypeLoadException.cs
Źródło:
TypeLoadException.cs
Źródło:
TypeLoadException.cs
Źródło:
TypeLoadException.cs

Inicjuje nowe wystąpienie TypeLoadException klasy z określonym komunikatem o błędzie.

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

Parametry

message
String

Komunikat opisujący błąd.

Przykłady

W poniższym przykładzie kodu pokazano TypeLoadException(String) konstruktor. Zawiera metodę, która generuje TypeLoadException element z komunikatem niestandardowym i wyświetla komunikat o błędzie w konsoli programu .

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.

Uwagi

Zawartość parametru message powinna być zrozumiała dla użytkownika. Obiekt wywołujący tego konstruktora jest wymagany, aby upewnić się, że ten ciąg został zlokalizowany dla bieżącej kultury systemu.

W poniższej tabeli przedstawiono początkowe wartości właściwości dla wystąpienia TypeLoadExceptionklasy .

Majątek Wartość
InnerException Odwołanie o wartości null (Nothing w Visual Basic).
Message Ciąg komunikatu o błędzie.

Dotyczy

TypeLoadException(SerializationInfo, StreamingContext)

Źródło:
TypeLoadException.cs
Źródło:
TypeLoadException.cs
Źródło:
TypeLoadException.cs
Źródło:
TypeLoadException.cs
Źródło:
TypeLoadException.cs

Uwaga

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

Inicjuje TypeLoadException nowe wystąpienie klasy z serializowanymi danymi.

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)

Parametry

info
SerializationInfo

Obiekt, który przechowuje serializowane dane obiektu.

context
StreamingContext

Kontekstowe informacje o źródle lub miejscu docelowym.

Atrybuty

Wyjątki

Obiekt info to null.

Przykłady

Poniższy przykład generuje wyjątek i serializuje dane wyjątku do pliku, a następnie ponownie tworzy wyjątek. W tym przykładzie kodu do uruchomienia należy podać w pełni kwalifikowaną nazwę zestawu. Aby uzyskać informacje na temat uzyskiwania w pełni kwalifikowanej nazwy zestawu, zobacz Nazwy zestawów.


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

Uwagi

Ten konstruktor jest wywoływany podczas deserializacji w celu ponownego utworzenia obiektu wyjątku przesyłanego przez strumień. Aby uzyskać więcej informacji, zobacz Serializacja XML i SOAP.

Zobacz też

Dotyczy

TypeLoadException(String, Exception)

Źródło:
TypeLoadException.cs
Źródło:
TypeLoadException.cs
Źródło:
TypeLoadException.cs
Źródło:
TypeLoadException.cs
Źródło:
TypeLoadException.cs

Inicjuje nowe wystąpienie TypeLoadException klasy z określonym komunikatem o błędzie i odwołaniem do wyjątku wewnętrznego, który jest przyczyną tego wyjątku.

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)

Parametry

message
String

Komunikat o błędzie wyjaśniający przyczynę wyjątku.

inner
Exception

Wyjątek, który jest przyczyną bieżącego wyjątku. inner Jeśli parametr nie nulljest , bieżący wyjątek jest zgłaszany w catch bloku, który obsługuje wyjątek wewnętrzny.

Przykłady

W poniższym przykładzie kodu pokazano TypeLoadException(String, Exception) konstruktor. Zawiera metodę, która generuje TypeLoadExceptionwyjątek , przechwytuje ten wyjątek i zgłasza nowy TypeLoadException komunikat niestandardowy, w tym oryginał TypeLoadException jako wyjątek wewnętrzny.

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

Uwagi

Wyjątek zgłaszany bezpośrednio w wyniku poprzedniego wyjątku może zawierać odwołanie do poprzedniego wyjątku InnerException we właściwości . Właściwość InnerException zwraca tę samą wartość, która jest przekazywana do konstruktora lub null jeśli InnerException właściwość nie dostarcza wartości wyjątku wewnętrznego do konstruktora.

W poniższej tabeli przedstawiono początkowe wartości właściwości dla wystąpienia TypeLoadExceptionklasy .

Majątek Wartość
InnerException Odwołanie do wyjątku wewnętrznego.
Message Ciąg komunikatu o błędzie.

Zobacz też

Dotyczy