Type.IsSerializable Type.IsSerializable Type.IsSerializable Type.IsSerializable Property

定義

取得值,指出 Type 是否可序列化。Gets a value indicating whether the Type is serializable.

public:
 virtual property bool IsSerializable { bool get(); };
public virtual bool IsSerializable { get; }
member this.IsSerializable : bool
Public Overridable 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);
            }
        }
    }
}
Imports System
Imports Microsoft.VisualBasic
Namespace SystemType
    Public Class [MyClass]
        ' Declare a public class with the [Serializable] attribute.
        <Serializable()> Public Class MyTestClass
        End Class 'MyTestClass
        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 'Main
    End Class '[MyClass]
End Namespace 'SystemType

備註

在 .NET Standard 中定義的類型不會以SerializableAttribute標記。Types 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的表示泛型型別或泛型方法定義中的類型參數, 則這個屬性false一律會傳回。If the current Type represents a type parameter in the definition of a generic type or generic method, this property always returns false.

適用於

另請參閱