Type.IsSerializable 属性

定义

获取一个值,通过该值指示 Type 是否为可序列化的。Gets a value indicating whether the Type is serializable.

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

属性值

如果 Type 是可序列化的,则为 true;否则为 falsetrue if the Type is serializable; otherwise, false.

实现

示例

下面的示例创建 MyTestClass 类的实例,设置 [Serializable] 特性,并检查 truefalseIsSerializable 属性。The following example creates an instance of MyTestClass class, sets the [Serializable] attribute, and checks the IsSerializable property for true or false.

using namespace System;
public ref class MyClass
{
public:

   // Declare a public class with the [Serializable] attribute.

   [Serializable]
   ref class MyTestClass{};


};

int main()
{
   try
   {
      bool myBool = false;
      MyClass::MyTestClass^ myTestClassInstance = gcnew MyClass::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 );
   }
   catch ( Exception^ e ) 
   {
      Console::WriteLine( "\nAn exception occurred: {0}", e->Message );
   }

}

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

注解

.NET Standard 中定义的类型未标记为 SerializableAttributeTypes that are defined in the .NET Standard are not marked with SerializableAttribute. 相反,每个 .NET 实现确定类型是否可序列化。Instead, each .NET implementation determines whether a type is serializable. 在运行时,可以使用 IsSerializable 属性来确定该实现是否支持对该类型的实例进行序列化。At run time, you can use the IsSerializable property to determine whether that implementation supports serialization of an instance of the type. 有关详细信息和示例,请参阅如何确定 .NET Standard 对象是否可以序列化For more information and an example, see How to determine if a .NET Standard object is serializable.

如果当前 Type 表示构造泛型类型,则此属性应用于构造该类型的泛型类型定义。If the current Type represents a constructed generic type, this property applies to the generic type definition from which the type was constructed. 例如,如果当前 Type 表示 MyGenericType<int>MyGenericType(Of Integer) Visual Basic),则此属性的值由 MyGenericType<T>确定。For example, if the current Type represents MyGenericType<int> (MyGenericType(Of Integer) in Visual Basic), the value of this property is determined by MyGenericType<T>.

如果当前 Type 表示泛型类型或泛型方法的定义中的类型参数,则此属性始终返回 falseIf the current Type represents a type parameter in the definition of a generic type or generic method, this property always returns false.

适用于

另请参阅