Type.IsSerializable Tulajdonság

Definíció

Egy értéket kap, amely jelzi, hogy a Type bináris szerializálható-e.

public:
 property bool IsSerializable { bool get(); };
public:
 virtual property bool IsSerializable { bool get(); };
public bool IsSerializable { get; }
public virtual bool IsSerializable { get; }
member this.IsSerializable : bool
Public ReadOnly Property IsSerializable As Boolean
Public Overridable ReadOnly Property IsSerializable As Boolean

Tulajdonság értéke

trueha a Type bináris szerializálható; ellenkező esetben. false

Megvalósítás

Példák

Az alábbi példa létrehoz egy osztálypéldánytMyTestClass, beállítja a [Szerializálható] attribútumot, és ellenőrzi a tulajdonságot vagy falsea IsSerializable tulajdonságottrue.

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

Megjegyzések

A .NET Standardban definiált típusok nincsenek megjelölve SerializableAttribute. Ehelyett minden .NET implementáció meghatározza, hogy egy típus bináris szerializálható-e. Futásidőben a tulajdonság használatával megállapíthatja, hogy az IsSerializable implementáció támogatja-e a típuspéldány bináris szerializálását. További információ és példa: Hogyan állapítsa meg, hogy egy .NET Standard objektum szerializálható-e.

Ha az áram Type egy létrehozott általános típust jelöl, ez a tulajdonság arra az általános típusdefinícióra vonatkozik, amelyből a típust létrehozták. Ha például az aktuális TypeMyGenericType<int> (MyGenericType(Of Integer) Visual Basic), akkor a tulajdonság értékét a MyGenericType<T> határozza meg.

Ha az aktuális Type érték egy típusparamétert jelöl egy általános vagy általános metódus definíciójában, ez a tulajdonság mindig ad vissza false.

A következőre érvényes:

Lásd még