IsNotSerialized Property

FieldInfo.IsNotSerialized Property

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

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

public bool IsNotSerialized { get; }
/** @property */
public final boolean get_IsNotSerialized ()

public final function get IsNotSerialized () : boolean

Not applicable.

Property Value

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.

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]);
    }
}

import System.*;
import System.Reflection.*;
import System.Runtime.Serialization.*;

public class MyClass
{
    public short myShort;
   
    // The following field will not be serialized.  
    /** @attribute NonSerialized()
     */
    public int myInt;
} //MyClass

public class TypeIsNotSerializable
{
    public static void main(String[] args)
    {
        // Get the type of MyClass.
        Type myType = MyClass.class.ToType();

        // 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].get_IsNotSerialized()) {
                Console.WriteLine("The {0} field is not serializable.",
                    myFields.get_Item(i));
            }
            else {
                Console.WriteLine("The {0} field is not serializable.",
                    myFields.get_Item(i));
            }
        }
    } //main 
} //TypeIsNotSerializable

Windows 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0

XNA Framework

Supported in: 1.0

Community Additions

ADD
Show:
© 2016 Microsoft