FieldInfo.IsNotSerialized FieldInfo.IsNotSerialized FieldInfo.IsNotSerialized FieldInfo.IsNotSerialized Property

定義

このフィールドに、NotSerialized 属性が設定されているかどうかを示す値を取得します。Gets a value indicating whether this field has the NotSerialized attribute.

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

プロパティ値

フィールドに true 属性が設定されている場合は NotSerialized。それ以外の場合は falsetrue if the field has the NotSerialized attribute set; otherwise, false.

実装

次の例では、MyClass のフィールドのフィールド情報を取得し、フィールドをシリアル化できるかどうかを判断し、結果を表示します。The following example gets the field information of the fields of MyClass, determines if the fields can be serialized, and displays the results.

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
Imports System.Reflection
Imports System.Runtime.Serialization
Imports Microsoft.VisualBasic

<Serializable()> _
Public Class [MyClass]
    Public myShort As Short

    ' The following field will not be serialized.  
    <NonSerialized()> Public myInt As Integer
End Class '[MyClass]

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 'Main
End Class 'Type_IsNotSerializable

注釈

フィールドにIsNotSerializedフラグが設定されている場合、プロパティはを返しtrueます。 FieldAttributes.NotSerializedThe IsNotSerialized property returns true when the field is marked with the FieldAttributes.NotSerialized flag. このフラグがフィールドに設定されている場合は、型がリモート処理されているときに、フィールドをシリアル化する必要がないことを示します。When this flag is set on a field, it indicates that the field does not have to be serialized when the type is remoted.

適用対象

こちらもご覧ください