FieldInfo.IsNotSerialized Property


Gets a value indicating whether this field has the NotSerialized attribute.

Namespace:   System.Reflection
Assembly:  mscorlib (in mscorlib.dll)

Public ReadOnly Property IsNotSerialized As Boolean

Property Value

Type: System.Boolean

true if the field has the NotSerialized attribute set; otherwise, false.

The 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.

The following example gets the field information of the fields of MyClass, determines if the fields can be serialized, and displays the results.

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)
        ' 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))
                Console.WriteLine("The {0} field is serializable.", myFields(i))
            End If
        Next i
    End Sub 'Main
End Class 'Type_IsNotSerializable

.NET Framework
Available since 1.1
Available since 2.0
Windows Phone Silverlight
Available since 7.0
Return to top