Type.IsSerializable 属性

定义

注意

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

获取一个值, Type 该值指示 是否可序列化二进制。

C#
public virtual bool IsSerializable { get; }
C#
[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; }
C#
public bool IsSerializable { get; }

属性值

true Type如果 是二进制可序列化的,则为 ;否则为 false

实现

属性

示例

以下示例创建 类的MyTestClass实例,设置 [Serializable] 属性,并检查 IsSerializablefalse的属性true

C#
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);
            }
        }
    }
}

注解

.NET Standard 中定义的类型未标记为 SerializableAttribute。 相反,每个 .NET 实现都会确定类型是否是二进制可序列化的。 在运行时,可以使用 IsSerializable 属性来确定该实现是否支持类型的实例的二进制序列化。 有关详细信息和示例,请参阅 如何确定 .NET Standard 对象是否可序列化

如果当前 Type 表示构造的泛型类型,则此属性适用于从中构造该类型的泛型类型定义。 例如,如果当前 Type 表示 MyGenericType<int> Visual Basic) MyGenericType(Of Integer) 中的 (,则此属性的值由 MyGenericType<T>确定。

如果当前 Type 表示泛型类型或泛型方法的定义中的类型参数,则此属性始终返回 false

适用于

产品 版本 (已过时)
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7 (8, 9)
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

另请参阅