Compartir por


Type.IsSerializable Propiedad

Definición

Precaución

Formatter-based serialization is obsolete and should not be used.

Obtiene un valor que indica si es Type serializable binario.

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

Valor de propiedad

true es si es Type serializable binario; en caso contrario, falsees .

Implementaciones

Atributos

Ejemplos

En el ejemplo siguiente se crea una instancia de MyTestClass clase , se establece el atributo [Serializable] y se comprueba la IsSerializable propiedad para true o 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

Comentarios

Los tipos definidos en .NET Standard no están marcados con SerializableAttribute. En su lugar, cada implementación de .NET determina si un tipo es serializable binario. En tiempo de ejecución, puede usar la IsSerializable propiedad para determinar si esa implementación admite la serialización binaria de una instancia del tipo . Para obtener más información y un ejemplo, vea Cómo determinar si un objeto .NET Standard es serializable.

Si el objeto actual Type representa un tipo genérico construido, esta propiedad se aplica a la definición de tipo genérico desde la que se construyó el tipo. Por ejemplo, si el objeto actual Type representa MyGenericType<int> (MyGenericType(Of Integer) en Visual Basic), el valor de esta propiedad viene determinado por MyGenericType<T>.

Si el objeto actual Type representa un parámetro de tipo en la definición de un tipo genérico o un método genérico, esta propiedad siempre devuelve false.

Se aplica a

Consulte también