TypeLoadException.GetObjectData(SerializationInfo, StreamingContext) Méthode
Définition
Important
Certaines informations portent sur la préversion du produit qui est susceptible d’être en grande partie modifiée avant sa publication. Microsoft exclut toute garantie, expresse ou implicite, concernant les informations fournies ici.
Attention
This API supports obsolete formatter-based serialization. It should not be called or extended by application code.
Définit l’objet SerializationInfo avec le nom de la classe, le nom de la méthode, l’ID de ressource et les informations d’exception complémentaires.
public:
override void GetObjectData(System::Runtime::Serialization::SerializationInfo ^ info, System::Runtime::Serialization::StreamingContext context);
public override void GetObjectData (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}")]
public override void GetObjectData (System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context);
[System.Security.SecurityCritical]
public override void GetObjectData (System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context);
override this.GetObjectData : System.Runtime.Serialization.SerializationInfo * System.Runtime.Serialization.StreamingContext -> unit
[<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}")>]
override this.GetObjectData : System.Runtime.Serialization.SerializationInfo * System.Runtime.Serialization.StreamingContext -> unit
[<System.Security.SecurityCritical>]
override this.GetObjectData : System.Runtime.Serialization.SerializationInfo * System.Runtime.Serialization.StreamingContext -> unit
Public Overrides Sub GetObjectData (info As SerializationInfo, context As StreamingContext)
Paramètres
- info
- SerializationInfo
Objet qui contient les données sérialisées de l'objet.
- context
- StreamingContext
Informations contextuelles sur la source ou la destination.
Implémente
- Attributs
Exceptions
L’objet info
est null
.
Exemples
L’exemple suivant génère une exception et sérialise les données d’exception dans un fichier, puis reconstitue l’exception. Pour que cet exemple de code s’exécute, vous devez fournir le nom complet de l’assembly. Pour plus d’informations sur la façon d’obtenir le nom complet de l’assembly, consultez Noms d’assembly.
#using <System.Runtime.Serialization.Formatters.Soap.dll>
using namespace System;
using namespace System::Reflection;
using namespace System::Runtime::Serialization;
using namespace System::Runtime::Serialization::Formatters::Soap;
using namespace System::IO;
// This class overrides the GetObjectData method and initializes
// its data with current time.
[Serializable]
public ref class MyTypeLoadExceptionChild: public TypeLoadException
{
public:
System::DateTime ErrorDateTime;
MyTypeLoadExceptionChild()
{
ErrorDateTime = DateTime::Now;
}
MyTypeLoadExceptionChild( DateTime myDateTime )
{
ErrorDateTime = myDateTime;
}
protected:
MyTypeLoadExceptionChild( SerializationInfo^ sInfo, StreamingContext * sContext )
{
// Reconstitute the deserialized information into the instance.
ErrorDateTime = sInfo->GetDateTime( "ErrorDate" );
}
public:
void GetObjectData( SerializationInfo^ sInfo, StreamingContext * sContext )
{
// Add a value to the Serialization information.
sInfo->AddValue( "ErrorDate", ErrorDateTime );
}
};
int main()
{
// Load the mscorlib assembly and get a reference to it.
// You must supply the fully qualified assembly name for mscorlib.dll here.
Assembly^ myAssembly = Assembly::Load( "Assembly text name, Version, Culture, PublicKeyToken" );
try
{
Console::WriteLine( "Attempting to load a type not present in the assembly 'mscorlib'" );
// This loading of invalid type raises a TypeLoadException
Type^ myType = myAssembly->GetType( "System::NonExistentType", true );
}
catch ( TypeLoadException^ )
{
// Serialize the exception to disk and reconstitute it back again.
try
{
System::DateTime ErrorDatetime = DateTime::Now;
Console::WriteLine( "A TypeLoadException has been raised." );
// Create MyTypeLoadException instance with current time.
MyTypeLoadExceptionChild^ myTypeLoadExceptionChild = gcnew MyTypeLoadExceptionChild( ErrorDatetime );
IFormatter^ myFormatter = gcnew SoapFormatter;
Stream^ myFileStream = gcnew FileStream( "typeload.xml",FileMode::Create,FileAccess::Write,FileShare::None );
Console::WriteLine( "Serializing the TypeLoadException with DateTime as {0}", ErrorDatetime );
// Serialize the MyTypeLoadException instance to a file.
myFormatter->Serialize( myFileStream, myTypeLoadExceptionChild );
myFileStream->Close();
Console::WriteLine( "Deserializing the Exception." );
myFileStream = gcnew FileStream( "typeload.xml",FileMode::Open,FileAccess::Read,FileShare::None );
// Deserialize and reconstitute the instance from file.
myTypeLoadExceptionChild = safe_cast<MyTypeLoadExceptionChild^>(myFormatter->Deserialize( myFileStream ));
myFileStream->Close();
Console::WriteLine( "Deserialized exception has ErrorDateTime = {0}", myTypeLoadExceptionChild->ErrorDateTime );
}
catch ( Exception^ e )
{
Console::WriteLine( "Exception : {0}", e->Message );
}
}
catch ( Exception^ e )
{
Console::WriteLine( "Exception : {0}", e->Message );
}
}
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
Remarques
GetObjectData définit un SerializationInfo avec toutes les données d'objet exception ciblées pour la sérialisation. Pendant la désérialisation, l’objet d’exception est reconstitué à partir du SerializationInfo transmis sur le flux.
Pour plus d’informations, consultez Sérialisation XML et SOAP.