FieldInfo.IsNotSerialized 属性
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
注意
Formatter-based serialization is obsolete and should not be used.
获取一个值,通过该值指示此字段是否有 NotSerialized
特性。
public:
property bool IsNotSerialized { bool get(); };
public bool IsNotSerialized { get; }
[System.Obsolete("Formatter-based serialization is obsolete and should not be used.", DiagnosticId="SYSLIB0050", UrlFormat="https://aka.ms/dotnet-warnings/{0}")]
public bool IsNotSerialized { get; }
member this.IsNotSerialized : bool
[<System.Obsolete("Formatter-based serialization is obsolete and should not be used.", DiagnosticId="SYSLIB0050", UrlFormat="https://aka.ms/dotnet-warnings/{0}")>]
member this.IsNotSerialized : bool
Public ReadOnly Property IsNotSerialized As Boolean
属性值
如果字段设置了 true
属性,则为 NotSerialized
;否则为 false
。
实现
- 属性
示例
以下示例获取 MyClass 字段的字段信息,确定是否可以序列化这些字段,并显示结果。
using namespace System;
using namespace System::Reflection;
using namespace System::Runtime::Serialization;
public ref class MyClass
{
public:
short myShort;
// The following field will not be serialized.
[NonSerialized]
int myInt;
};
int main()
{
// Get the type of MyClass.
Type^ myType = MyClass::typeid;
// Get the fields of MyClass.
array<FieldInfo^>^myFields = myType->GetFields( static_cast<BindingFlags>(BindingFlags::Public | BindingFlags::NonPublic | BindingFlags::Instance | BindingFlags::Static) );
Console::WriteLine( "\nDisplaying whether or not the field is serializable.\n" );
// Display whether or not the field is serializable.
for ( int i = 0; i < myFields->Length; i++ )
if ( myFields[ i ]->IsNotSerialized )
Console::WriteLine( "The {0} field is not serializable.", myFields[ i ] );
else
Console::WriteLine( "The {0} field is serializable.", myFields[ i ] );
}
using System;
using System.Reflection;
using System.Runtime.Serialization;
public class MyClass
{
public short myShort;
// The following field will not be serialized.
[NonSerialized()]
public int myInt;
}
public class Type_IsNotSerializable
{
public static void Main()
{
// Get the type of MyClass.
Type myType = typeof(MyClass);
// Get the fields of MyClass.
FieldInfo[] myFields = myType.GetFields(BindingFlags.Public |
BindingFlags.NonPublic |
BindingFlags.Instance |
BindingFlags.Static);
Console.WriteLine("\nDisplaying whether or not the field is serializable.\n");
// Display whether or not the field is serializable.
for(int i = 0; i < myFields.Length; i++)
if(myFields[i].IsNotSerialized)
Console.WriteLine("The {0} field is not serializable.", myFields[i]);
else
Console.WriteLine("The {0} field is not serializable.", myFields[i]);
}
}
Imports System.Reflection
Imports System.Runtime.Serialization
<Serializable()> _
Public Class [MyClass]
Public myShort As Short
' The following field will not be serialized.
<NonSerialized()> Public myInt As Integer
End Class
Public Class Type_IsNotSerializable
Public Shared Sub Main()
' Get the type of MyClass.
Dim myType As Type = GetType([MyClass])
' Get the fields of MyClass.
Dim myFields As FieldInfo() = myType.GetFields((BindingFlags.Public Or BindingFlags.NonPublic Or BindingFlags.Instance Or BindingFlags.Static))
Console.WriteLine(ControlChars.Cr & "Displaying whether or not the field is serializable." & ControlChars.Cr)
Console.WriteLine()
' Displaying whether or not the field is serializable.
Dim i As Integer
For i = 0 To myFields.Length - 1
If myFields(i).IsNotSerialized Then
Console.WriteLine("The {0} field is not serializable.", myFields(i))
Else
Console.WriteLine("The {0} field is serializable.", myFields(i))
End If
Next i
End Sub
End Class
注解
当IsNotSerialized
用 标志标记字段时, FieldAttributes.NotSerialized
属性返回true
。 在字段上设置此标志时,它指示在远程类型时不必序列化该字段。