Type.IsSerializable 屬性
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
警告
Formatter-based serialization is obsolete and should not be used.
取得值,指出 是否 Type 可序列化二進位。
public:
virtual property bool IsSerializable { bool get(); };
public:
property bool IsSerializable { bool get(); };
public virtual bool IsSerializable { 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 bool IsSerializable { get; }
member this.IsSerializable : bool
[<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
Public Overridable ReadOnly Property IsSerializable As Boolean
Public ReadOnly Property IsSerializable As Boolean
屬性值
true
Type如果 為二進位可序列化,則為 ,否則為 false
。
實作
- 屬性
範例
下列範例會建立 類別的 MyTestClass
實例、設定 [Serializable] 屬性,並檢查 IsSerializable
或 false
的屬性 true
。
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);
}
}
}
}
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
備註
.NET Standard 中定義的型別不會標示為 SerializableAttribute 。 相反地,每個 .NET 實作都會判斷類型是否為二進位可序列化。 在執行時間,您可以使用 IsSerializable 屬性來判斷該實作是否支援類型的實例二進位序列化。 如需詳細資訊和範例,請參閱 如何判斷 .NET Standard 物件是否可序列化。
如果目前 Type 代表建構的泛型型別,則此屬性會套用至建構型別的泛型型別定義。 例如,如果目前的 Type 代表 MyGenericType<int>
Visual Basic) 中的 (MyGenericType(Of Integer)
,則此屬性的值是由 MyGenericType<T>
決定。
如果目前的 Type 代表泛型型別或泛型方法定義中的型別參數,這個屬性一律會傳 false
回 。