Type.IsSerializable Vlastnost
Definice
Důležité
Některé informace platí pro předběžně vydaný produkt, který se může zásadně změnit, než ho výrobce nebo autor vydá. Microsoft neposkytuje žádné záruky, výslovné ani předpokládané, týkající se zde uváděných informací.
Upozornění
Formatter-based serialization is obsolete and should not be used.
Získá hodnotu určující, zda Type je binární serializovatelný.
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
Hodnota vlastnosti
true
Type pokud je binární serializovatelný; jinak , false.
Implementuje
- Atributy
Příklady
Následující příklad vytvoří instanci MyTestClass třídy, nastaví [Serializable] atribut a zkontroluje IsSerializable vlastnost pro true nebo false.
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
Poznámky
Typy definované v rozhraní .NET Standard nejsou označené SerializableAttribute. Místo toho každá implementace .NET určuje, zda je typ binární serializovatelný. Za běhu můžete pomocí IsSerializable vlastnosti určit, zda implementace podporuje binární serializaci instance typu. Další informace a příklad naleznete v tématu Jak zjistit, zda je objekt .NET Standard serializovatelný.
Pokud aktuální Type představuje vytvořený obecný typ, tato vlastnost se vztahuje na definici obecného typu, ze které byl typ vytvořen. Například pokud aktuální Type představuje MyGenericType<int> (MyGenericType(Of Integer) v jazyce Visual Basic), hodnota této vlastnosti je určena MyGenericType<T>.
Pokud aktuální Type představuje parametr typu v definici obecného typu nebo obecné metody, tato vlastnost vždy vrátí false.