Type.IsSerializable Property

Definition

Načte hodnotu, která označuje, zda je Type serializovatelný.Gets a value indicating whether the Type is serializable.

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

Property Value

Boolean

true, pokud je Type serializovatelný; v opačném případě false.true if the Type is serializable; otherwise, false.

Implements

Examples

Následující příklad vytvoří instanci třídy MyTestClass, nastaví atribut [serializovatelný] a kontroluje vlastnost IsSerializable pro true nebo false.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

Remarks

Typy, které jsou definovány v .NET Standard nejsou označeny jako SerializableAttribute.Types that are defined in the .NET Standard are not marked with SerializableAttribute. Místo toho Každá implementace rozhraní .NET určuje, zda je typ serializovatelný.Instead, each .NET implementation determines whether a type is serializable. V době běhu můžete použít vlastnost IsSerializable k určení, zda tato implementace podporuje serializaci instance typu.At run time, you can use the IsSerializable property to determine whether that implementation supports serialization of an instance of the type. Další informace a příklad najdete v tématu jak určit, zda je objekt .NET Standard serializovatelný.For more information and an example, see How to determine if a .NET Standard object is serializable.

Pokud aktuální Type představuje Konstruovaný obecný typ, tato vlastnost se vztahuje na definici obecného typu, ze které byl typ vytvořen.If the current Type represents a constructed generic type, this property applies to the generic type definition from which the type was constructed. Pokud například aktuální Type představuje MyGenericType<int> (MyGenericType(Of Integer) v Visual Basic), hodnota této vlastnosti je určena 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>.

Pokud aktuální Type představuje parametr typu v definici obecného typu nebo obecné metody, tato vlastnost vždy vrátí false.If the current Type represents a type parameter in the definition of a generic type or generic method, this property always returns false.

Applies to

See also