SerializationInfo Klasa
Definicja
Ważne
Niektóre informacje odnoszą się do produktu w wersji wstępnej, który może zostać znacząco zmodyfikowany przed wydaniem. Firma Microsoft nie udziela żadnych gwarancji, jawnych lub domniemanych, w odniesieniu do informacji podanych w tym miejscu.
Przechowuje wszystkie dane potrzebne do serializacji lub deserializacji obiektu. Klasa ta nie może być dziedziczona.
public ref class SerializationInfo sealed
public sealed class SerializationInfo
[System.Runtime.InteropServices.ComVisible(true)]
public sealed class SerializationInfo
type SerializationInfo = class
[<System.Runtime.InteropServices.ComVisible(true)>]
type SerializationInfo = class
Public NotInheritable Class SerializationInfo
- Dziedziczenie
-
SerializationInfo
- Atrybuty
Przykłady
W poniższym przykładzie kodu przedstawiono SerializationInfo niestandardową serializacji i deserializacji różnych wartości.
using namespace System;
using namespace System::IO;
using namespace System::Collections;
using namespace System::Runtime::Serialization::Formatters::Binary;
using namespace System::Runtime::Serialization;
ref class SingletonSerializationHelper;
// There should be only one instance of this type per AppDomain.
[Serializable]
public ref class Singleton sealed: public ISerializable
{
private:
// This is the one instance of this type.
static Singleton^ theOneObject = gcnew Singleton;
public:
// Here are the instance fields.
String^ someString;
Int32 someNumber;
private:
// Private constructor allowing this type to construct the singleton.
Singleton()
{
// Do whatever is necessary to initialize the singleton.
someString = "This is a String* field";
someNumber = 123;
}
public:
// A method returning a reference to the singleton.
static Singleton^ GetSingleton()
{
return theOneObject;
}
// A method called when serializing a Singleton.
[System::Security::Permissions::SecurityPermissionAttribute
(System::Security::Permissions::SecurityAction::LinkDemand,
Flags=System::Security::Permissions::SecurityPermissionFlag::SerializationFormatter)]
virtual void GetObjectData( SerializationInfo^ info, StreamingContext context )
{
// Instead of serializing this Object*, we will
// serialize a SingletonSerializationHelp instead.
info->SetType( SingletonSerializationHelper::typeid );
// No other values need to be added.
}
// NOTE: ISerializable*'s special constructor is NOT necessary
// because it's never called
};
[Serializable]
private ref class SingletonSerializationHelper sealed: public IObjectReference
{
public:
// This Object* has no fields (although it could).
// GetRealObject is called after this Object* is deserialized
virtual Object^ GetRealObject( StreamingContext context )
{
// When deserialiing this Object*, return a reference to
// the singleton Object* instead.
return Singleton::GetSingleton();
}
};
[STAThread]
int main()
{
FileStream^ fs = gcnew FileStream( "DataFile.dat",FileMode::Create );
try
{
// Construct a BinaryFormatter and use it
// to serialize the data to the stream.
BinaryFormatter^ formatter = gcnew BinaryFormatter;
// Create an array with multiple elements refering to
// the one Singleton Object*.
array<Singleton^>^a1 = {Singleton::GetSingleton(),Singleton::GetSingleton()};
// This displays S"True".
Console::WriteLine( "Do both array elements refer to the same Object? {0}", (a1[ 0 ] == a1[ 1 ]) );
// Serialize the array elements.
formatter->Serialize( fs, a1 );
// Deserialize the array elements.
fs->Position = 0;
array<Singleton^>^a2 = (array<Singleton^>^)formatter->Deserialize( fs );
// This displays S"True".
Console::WriteLine( "Do both array elements refer to the same Object? {0}", (a2[ 0 ] == a2[ 1 ]) );
// This displays S"True".
Console::WriteLine( "Do all array elements refer to the same Object? {0}", (a1[ 0 ] == a2[ 0 ]) );
}
catch ( SerializationException^ e )
{
Console::WriteLine( "Failed to serialize. Reason: {0}", e->Message );
throw;
}
finally
{
fs->Close();
}
return 0;
}
using System;
using System.Text;
using System.IO;
// Add references to Soap and Binary formatters.
using System.Runtime.Serialization.Formatters.Binary;
using System.Runtime.Serialization.Formatters.Soap ;
using System.Runtime.Serialization;
[Serializable]
public class MyItemType : ISerializable
{
public MyItemType()
{
// Empty constructor required to compile.
}
// The value to serialize.
private string myProperty_value;
public string MyProperty
{
get { return myProperty_value; }
set { myProperty_value = value; }
}
// Implement this method to serialize data. The method is called
// on serialization.
public void GetObjectData(SerializationInfo info, StreamingContext context)
{
// Use the AddValue method to specify serialized values.
info.AddValue("props", myProperty_value, typeof(string));
}
// The special constructor is used to deserialize values.
public MyItemType(SerializationInfo info, StreamingContext context)
{
// Reset the property value using the GetValue method.
myProperty_value = (string) info.GetValue("props", typeof(string));
}
}
// This is a console application.
public static class Test
{
static void Main()
{
// This is the name of the file holding the data. You can use any file extension you like.
string fileName = "dataStuff.myData";
// Use a BinaryFormatter or SoapFormatter.
IFormatter formatter = new BinaryFormatter();
//IFormatter formatter = new SoapFormatter();
Test.SerializeItem(fileName, formatter); // Serialize an instance of the class.
Test.DeserializeItem(fileName, formatter); // Deserialize the instance.
Console.WriteLine("Done");
Console.ReadLine();
}
public static void SerializeItem(string fileName, IFormatter formatter)
{
// Create an instance of the type and serialize it.
MyItemType t = new MyItemType();
t.MyProperty = "Hello World";
FileStream s = new FileStream(fileName , FileMode.Create);
formatter.Serialize(s, t);
s.Close();
}
public static void DeserializeItem(string fileName, IFormatter formatter)
{
FileStream s = new FileStream(fileName, FileMode.Open);
MyItemType t = (MyItemType)formatter.Deserialize(s);
Console.WriteLine(t.MyProperty);
}
}
Imports System.Text
Imports System.IO
' Add references to Soap and Binary formatters.
Imports System.Runtime.Serialization.Formatters.Binary
Imports System.Runtime.Serialization.Formatters.Soap
Imports System.Runtime.Serialization
<Serializable()> _
Public Class MyItemType
Implements ISerializable
' Empty constructor required to compile.
Public Sub New()
End Sub
' The value to serialize.
Private myProperty_value As String
Public Property MyProperty() As String
Get
Return myProperty_value
End Get
Set(value As String)
myProperty_value = value
End Set
End Property
' Implement this method to serialize data. The method is called
' on serialization.
Public Sub GetObjectData(info As SerializationInfo, context As StreamingContext) Implements ISerializable.GetObjectData
' Use the AddValue method to specify serialized values.
info.AddValue("props", myProperty_value, GetType(String))
End Sub
' The special constructor is used to deserialize values.
Public Sub New(info As SerializationInfo, context As StreamingContext)
' Reset the property value using the GetValue method.
myProperty_value = DirectCast(info.GetValue("props", GetType(String)), String)
End Sub
End Class
' This is a console application.
Public Class Test
Public Shared Sub Main()
' This is the name of the file holding the data. You can use any file extension you like.
Dim fileName As String = "dataStuff.myData"
' Use a BinaryFormatter or SoapFormatter.
Dim formatter As IFormatter = New BinaryFormatter()
' Dim formatter As IFormatter = New SoapFormatter()
Test.SerializeItem(fileName, formatter)
' Serialize an instance of the class.
Test.DeserializeItem(fileName, formatter)
' Deserialize the instance.
Console.WriteLine("Done")
Console.ReadLine()
End Sub
Public Shared Sub SerializeItem(fileName As String, formatter As IFormatter)
' Create an instance of the type and serialize it.
Dim myType As New MyItemType()
myType.MyProperty = "Hello World"
Dim fs As New FileStream(fileName, FileMode.Create)
formatter.Serialize(fs, myType)
fs.Close()
End Sub
Public Shared Sub DeserializeItem(fileName As String, formatter As IFormatter)
Dim fs As New FileStream(fileName, FileMode.Open)
Dim myType As MyItemType = DirectCast(formatter.Deserialize(fs), MyItemType)
Console.WriteLine(myType.MyProperty)
End Sub
End Class
Uwagi
Ta klasa jest używana przez obiekty z niestandardowym zachowaniem serializacji. Metoda GetObjectData w magazynie ISerializable lub ISerializationSurrogate wypełnia SerializationInfo magazyn nazwą, typem i wartością każdego elementu informacji, które chce serializować. Podczas deserializacji odpowiednia funkcja może wyodrębnić te informacje.
Obiekty są dodawane do SerializationInfo magazynu w czasie serializacji przy użyciu AddValue metod i wyodrębnione ze sklepu SerializationInfo w deserializacji przy użyciu GetValue metod.
Aby uzyskać więcej informacji na temat dostosowywania serializacji, zobacz Custom Serialization (Serializacja niestandardowa).
Konstruktory
SerializationInfo(Type, IFormatterConverter) |
Tworzy nowe wystąpienie klasy SerializationInfo. |
SerializationInfo(Type, IFormatterConverter, Boolean) |
Inicjuje nowe wystąpienie klasy SerializationInfo. |
Właściwości
AssemblyName |
Pobiera lub ustawia nazwę zestawu typu do serializacji tylko podczas serializacji. |
FullTypeName |
Pobiera lub ustawia pełną nazwę elementu Type do serializacji. |
IsAssemblyNameSetExplicit |
Pobiera, czy nazwa zestawu została jawnie ustawiona. |
IsFullTypeNameSetExplicit |
Pobiera, czy nazwa pełnego typu została jawnie ustawiona. |
MemberCount |
Pobiera liczbę członków, które zostały dodane do sklepu SerializationInfo . |
ObjectType |
Zwraca typ obiektu do serializacji. |
Metody
AddValue(String, Boolean) |
Dodaje wartość logiczną do SerializationInfo magazynu. |
AddValue(String, Byte) |
Dodaje 8-bitową wartość całkowitą bez znaku do SerializationInfo magazynu. |
AddValue(String, Char) |
Dodaje wartość znaku Unicode do SerializationInfo magazynu. |
AddValue(String, DateTime) |
DateTime Dodaje wartość do SerializationInfo magazynu. |
AddValue(String, Decimal) |
Dodaje wartość dziesiętną do SerializationInfo magazynu. |
AddValue(String, Double) |
Dodaje do magazynu wartość zmiennoprzecinkową o podwójnej precyzji SerializationInfo . |
AddValue(String, Int16) |
Dodaje 16-bitową wartość całkowitą ze znakiem SerializationInfo do magazynu. |
AddValue(String, Int32) |
Dodaje 32-bitową wartość całkowitą ze znakiem SerializationInfo do magazynu. |
AddValue(String, Int64) |
Dodaje 64-bitową wartość całkowitą ze znakiem SerializationInfo do magazynu. |
AddValue(String, Object) |
Dodaje określony obiekt do SerializationInfo magazynu, gdzie jest skojarzony z określoną nazwą. |
AddValue(String, Object, Type) |
Dodaje wartość do SerializationInfo magazynu, z którym |
AddValue(String, SByte) |
Dodaje 8-bitową wartość całkowitą ze znakiem SerializationInfo do magazynu. |
AddValue(String, Single) |
Dodaje wartość zmiennoprzecinkową o pojedynczej precyzji SerializationInfo do magazynu. |
AddValue(String, UInt16) |
Dodaje 16-bitową niepodpisaną wartość całkowitą do SerializationInfo magazynu. |
AddValue(String, UInt32) |
Dodaje 32-bitową niepodpisaną wartość całkowitą do SerializationInfo magazynu. |
AddValue(String, UInt64) |
Dodaje 64-bitową niepodpisaną wartość całkowitą do SerializationInfo magazynu. |
Equals(Object) |
Określa, czy dany obiekt jest taki sam, jak bieżący obiekt. (Odziedziczone po Object) |
GetBoolean(String) |
Pobiera wartość logiczną ze sklepu SerializationInfo . |
GetByte(String) |
Pobiera 8-bitową niepodpisaną wartość całkowitą ze sklepu SerializationInfo . |
GetChar(String) |
Pobiera wartość znaku Unicode ze sklepu SerializationInfo . |
GetDateTime(String) |
DateTime Pobiera wartość ze sklepuSerializationInfo. |
GetDecimal(String) |
Pobiera wartość dziesiętną ze sklepu SerializationInfo . |
GetDouble(String) |
Pobiera z magazynu wartość zmiennoprzecinkową o podwójnej precyzji SerializationInfo . |
GetEnumerator() |
Zwraca wartość używaną SerializationInfoEnumerator do iterowania przez pary name-value w SerializationInfo magazynie. |
GetHashCode() |
Służy jako domyślna funkcja skrótu. (Odziedziczone po Object) |
GetInt16(String) |
Pobiera 16-bitową wartość całkowitą ze znakiem ze sklepu SerializationInfo . |
GetInt32(String) |
Pobiera 32-bitową wartość całkowitą ze znakiem ze sklepu SerializationInfo . |
GetInt64(String) |
Pobiera 64-bitową wartość całkowitą ze znakiem SerializationInfo z magazynu. |
GetSByte(String) |
Pobiera 8-bitową wartość całkowitą ze znakiem ze sklepu SerializationInfo . |
GetSingle(String) |
Pobiera wartość zmiennoprzecinkowa o pojedynczej precyzji SerializationInfo z magazynu. |
GetString(String) |
String Pobiera wartość ze sklepuSerializationInfo. |
GetType() |
Type Pobiera bieżące wystąpienie. (Odziedziczone po Object) |
GetUInt16(String) |
Pobiera 16-bitową niepodpisaną wartość całkowitą ze sklepu SerializationInfo . |
GetUInt32(String) |
Pobiera 32-bitową niepodpisaną wartość całkowitą ze sklepu SerializationInfo . |
GetUInt64(String) |
Pobiera 64-bitową niepodpisaną wartość całkowitą ze sklepu SerializationInfo . |
GetValue(String, Type) |
Pobiera wartość ze sklepu SerializationInfo . |
MemberwiseClone() |
Tworzy płytkią kopię bieżącego Objectelementu . (Odziedziczone po Object) |
SetType(Type) |
Type Ustawia obiekt na serializacji. |
ToString() |
Zwraca ciąg reprezentujący bieżący obiekt. (Odziedziczone po Object) |