Type.IsSerializable Eigenschaft
Definition
Wichtig
Einige Informationen beziehen sich auf Vorabversionen, die vor dem Release ggf. grundlegend überarbeitet werden. Microsoft übernimmt hinsichtlich der hier bereitgestellten Informationen keine Gewährleistungen, seien sie ausdrücklich oder konkludent.
Achtung
Formatter-based serialization is obsolete and should not be used.
Ruft einen Wert ab, der angibt, ob die Type binär serialisierbar ist.
public:
virtual property bool IsSerializable { bool get(); };
public:
property bool IsSerializable { bool get(); };
[System.Obsolete("Formatter-based serialization is obsolete and should not be used.", DiagnosticId="SYSLIB0050", UrlFormat="https://aka.ms/dotnet-warnings/{0}")]
public virtual bool IsSerializable { get; }
public virtual bool IsSerializable { get; }
public bool IsSerializable { get; }
[<System.Obsolete("Formatter-based serialization is obsolete and should not be used.", DiagnosticId="SYSLIB0050", UrlFormat="https://aka.ms/dotnet-warnings/{0}")>]
member this.IsSerializable : bool
member this.IsSerializable : bool
Public Overridable ReadOnly Property IsSerializable As Boolean
Public ReadOnly Property IsSerializable As Boolean
Eigenschaftswert
truewenn die Type binär serialisierbar ist; andernfalls . false
Implementiert
- Attribute
Beispiele
Im folgenden Beispiel wird eine Instanz der MyTestClass Klasse erstellt, das Attribut [Serializable] festgelegt und die IsSerializable Eigenschaft auf true oder falseüberprüft.
using System;
namespace SystemType
{
public class MyClass
{
// Declare a public class with the [Serializable] attribute.
[Serializable] public class MyTestClass
{
}
public static void Main(string []args)
{
try
{
bool myBool = false;
MyTestClass myTestClassInstance = new MyTestClass();
// Get the type of myTestClassInstance.
Type myType = myTestClassInstance.GetType();
// Get the IsSerializable property of myTestClassInstance.
myBool = myType.IsSerializable;
Console.WriteLine("\nIs {0} serializable? {1}.", myType.FullName, myBool.ToString());
}
catch (Exception e)
{
Console.WriteLine("\nAn exception occurred: {0}", e.Message);
}
}
}
}
open System
// Declare a public class with the [Serializable] attribute.
[<Serializable>]
type MyTestClass() = class end
try
let myTestClassInstance = MyTestClass()
// Get the type of myTestClassInstance.
let myType = myTestClassInstance.GetType()
// Get the IsSerializable property of myTestClassInstance.
let myBool = myType.IsSerializable
printfn $"\nIs {myType.FullName} serializable? {myBool}."
with e ->
printfn $"\nAn exception occurred: {e.Message}"
Namespace SystemType
Public Class [MyClass]
' Declare a public class with the [Serializable] attribute.
<Serializable()> Public Class MyTestClass
End Class
Public Overloads Shared Sub Main()
Try
Dim myBool As Boolean = False
Dim myTestClassInstance As New MyTestClass()
' Get the type of myTestClassInstance.
Dim myType As Type = myTestClassInstance.GetType()
' Get the IsSerializable property of myTestClassInstance.
myBool = myType.IsSerializable
Console.WriteLine(ControlChars.Cr + "Is {0} serializable? {1}.", myType.FullName, myBool.ToString())
Catch e As Exception
Console.WriteLine(ControlChars.Cr + "An exception occurred: {0}", e.Message.ToString())
End Try
End Sub
End Class
End Namespace 'SystemType
Hinweise
Typen, die in .NET Standard definiert sind, werden nicht mit SerializableAttribute. Stattdessen bestimmt jede .NET-Implementierung, ob ein Typ binär serialisierbar ist. Zur Laufzeit können Sie mithilfe der IsSerializable Eigenschaft ermitteln, ob diese Implementierung die binäre Serialisierung einer Instanz des Typs unterstützt. Weitere Informationen und ein Beispiel finden Sie unter How to determine if a .NET Standard object is serializable.
Wenn der aktuelle typ einen konstruierten generischen Type Typ darstellt, gilt diese Eigenschaft für die generische Typdefinition, aus der der Typ erstellt wurde. Wenn beispielsweise der aktuelle Type Wert (MyGenericType(Of Integer) in Visual Basic) darstellt MyGenericType<int> , wird der Wert dieser Eigenschaft durch MyGenericType<T>bestimmt.
Wenn der aktuelle Parameter in der Definition eines generischen Typs oder einer generischen Type Methode darstellt, gibt diese Eigenschaft immer zurück false.