Type.IsSerializable Egenskap
Definition
Viktigt
En del information gäller för förhandsversionen av en produkt och kan komma att ändras avsevärt innan produkten blir allmänt tillgänglig. Microsoft lämnar inga garantier, uttryckliga eller underförstådda, avseende informationen som visas här.
Varning
Formatter-based serialization is obsolete and should not be used.
Hämtar ett värde som anger om Type är binär serialiserbar.
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
Egenskapsvärde
true
Type om är binär serialiserbar, annars . false
Implementeringar
- Attribut
Exempel
I följande exempel skapas en instans av MyTestClass klassen, attributet [Serializable] anges och egenskapen kontrolleras IsSerializable för true eller 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
Kommentarer
Typer som definieras i .NET Standard är inte markerade med SerializableAttribute. I stället avgör varje .NET implementering om en typ är binär serialiserbar. Vid körning kan du använda IsSerializable egenskapen för att avgöra om implementeringen stöder binär serialisering av en instans av typen. Mer information och ett exempel finns i How to determine if a .NET Standard object is serializable.
Om strömmen Type representerar en konstruerad allmän typ gäller den här egenskapen för den generiska typdefinition som typen skapades från. Om den aktuella Type till exempel representerar MyGenericType<int> (MyGenericType(Of Integer) i Visual Basic) bestäms värdet för den här egenskapen av MyGenericType<T>.
Om strömmen Type representerar en typparameter i definitionen av en allmän typ eller allmän metod returnerar falseden här egenskapen alltid .