Export (0) Print
Expand All

NonSerializedAttribute Class

Indicates that a field of a serializable class should not be serialized. This class cannot be inherited.

For a list of all members of this type, see NonSerializedAttribute Members.

System.Object
   System.Attribute
      System.NonSerializedAttribute

[Visual Basic]
<AttributeUsage(AttributeTargets.Field)>
NotInheritable Public Class NonSerializedAttribute
   Inherits Attribute
[C#]
[AttributeUsage(AttributeTargets.Field)]
public sealed class NonSerializedAttribute : Attribute
[C++]
[AttributeUsage(AttributeTargets::Field)]
public __gc __sealed class NonSerializedAttribute : public
   Attribute
[JScript]
public
   AttributeUsage(AttributeTargets.Field)
class NonSerializedAttribute extends Attribute

Thread Safety

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Remarks

The target objects for the NonSerializedAttribute are public and private fields of a serializable class. By default, classes are not serializable unless they are marked with the SerializableAttribute. During the serialization process all the public and private fields of a class are serialized by default. Fields that are not to be serialized can be marked with the NonSerializedAttribute, which causes the serialization process to exclude the target field during serialization. Alternatively, implement the ISerializable interface to explicitly control the serialization process. Note that classes which implement ISerializable still need to be marked with the SerializableAttribute.

For more information about using attributes, see Extending Metadata Using Attributes.

Example

[Visual Basic, C#, C++] The following example demonstrates serialization of an object marked with the SerializableAttribute attribute, and the behavior of a field marked with the NonSerializedAttribute in the serialized object. To use the BinaryFormatter instead of the SoapFormatter, uncomment the appropriate lines.

[Visual Basic] 
Imports System
Imports System.IO
Imports System.Runtime.Serialization
Imports System.Runtime.Serialization.Formatters.Soap

''Imports System.Runtime.Serialization.Formatters.Binary


Public Class Test
   
   Public Shared Sub Main()
      
      ' Creates a new TestSimpleObject object.
      Dim obj As New TestSimpleObject()
      
      Console.WriteLine("Before serialization the object contains: ")
      obj.Print()
      
      ' Opens a file and serializes the object into it in binary format.
      Dim stream As Stream = File.Open("data.xml", FileMode.Create)
      Dim formatter As New SoapFormatter()
      
      ''Dim formatter As New BinaryFormatter()

      formatter.Serialize(stream, obj)
      stream.Close()
      
      ' Empties obj.
      obj = Nothing
      
      ' Opens file "data.xml" and deserializes the object from it.
      stream = File.Open("data.xml", FileMode.Open)
      formatter = New SoapFormatter()

      ''formatter = New BinaryFormatter()

      obj = CType(formatter.Deserialize(stream), TestSimpleObject)
      stream.Close()
      
      Console.WriteLine("")
      Console.WriteLine("After deserialization the object contains: ")
      obj.Print()

   End Sub 'Main

End Class 'Test


' A test object that needs to be serialized.
<Serializable()> Public Class TestSimpleObject
   
   Public member1 As Integer
   Public member2 As String
   Public member3 As String
   Public member4 As Double
   
   ' A member that is not serialized.
   <NonSerialized()> Public member5 As String  
  
   
   Public Sub New()     
      member1 = 11
      member2 = "hello"
      member3 = "hello"
      member4 = 3.14159265
      member5 = "hello world!"
   End Sub 'New
      
   
   Public Sub Print()      
      Console.WriteLine("member1 = '{0}'", member1)
      Console.WriteLine("member2 = '{0}'", member2)
      Console.WriteLine("member3 = '{0}'", member3)
      Console.WriteLine("member4 = '{0}'", member4)
      Console.WriteLine("member5 = '{0}'", member5)
   End Sub 'Print

End Class 'TestSimpleObject

[C#] 
using System;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Soap;
//using System.Runtime.Serialization.Formatters.Binary;

public class Test {
   public static void Main()  {

      //Creates a new TestSimpleObject object.
      TestSimpleObject obj = new TestSimpleObject();

      Console.WriteLine("Before serialization the object contains: ");
      obj.Print();

      //Opens a file and serializes the object into it in binary format.
      Stream stream = File.Open("data.xml", FileMode.Create);
      SoapFormatter formatter = new SoapFormatter();

      //BinaryFormatter formatter = new BinaryFormatter();

      formatter.Serialize(stream, obj);
      stream.Close();
   
      //Empties obj.
      obj = null;
   
      //Opens file "data.xml" and deserializes the object from it.
      stream = File.Open("data.xml", FileMode.Open);
      formatter = new SoapFormatter();

      //formatter = new BinaryFormatter();

      obj = (TestSimpleObject)formatter.Deserialize(stream);
      stream.Close();

      Console.WriteLine("");
      Console.WriteLine("After deserialization the object contains: ");
      obj.Print();
   }
}


// A test object that needs to be serialized.
[Serializable()]        
public class TestSimpleObject  {

    public int member1;
    public string member2;
    public string member3;
    public double member4;
    
    // A field that is not serialized.
    [NonSerialized()] public string member5; 
    
    public TestSimpleObject() {

        member1 = 11;
        member2 = "hello";
        member3 = "hello";
        member4 = 3.14159265;
        member5 = "hello world!";
    }


    public void Print() {

        Console.WriteLine("member1 = '{0}'", member1);
        Console.WriteLine("member2 = '{0}'", member2);
        Console.WriteLine("member3 = '{0}'", member3);
        Console.WriteLine("member4 = '{0}'", member4);
        Console.WriteLine("member5 = '{0}'", member5);
    }
}

[C++] 
#using <mscorlib.dll>
#using <system.dll>
#using <system.messaging.dll>
#using <System.Runtime.Serialization.Formatters.Soap.dll>

using namespace System;
using namespace System::IO;
using namespace System::Runtime::Serialization::Formatters::Soap;

// A test object that needs to be serialized.
[Serializable]        
__gc class TestSimpleObject 
{
    int member1;
    String* member2;
    String* member3;
    double member4;
public:
    // A field that is not serialized.
    [NonSerialized] String* member5; 

    TestSimpleObject() 
    {
        member1 = 11;
        member2 = S"hello";
        member3 = S"hello";
        member4 = 3.14159265;
        member5 = S"hello world!";
    }

    void Print() 
    {
        Console::WriteLine(S"member1 = ' {0}'", __box(member1));
        Console::WriteLine(S"member2 = ' {0}'", member2);
        Console::WriteLine(S"member3 = ' {0}'", member3);
        Console::WriteLine(S"member4 = ' {0}'", __box(member4));
        Console::WriteLine(S"member5 = ' {0}'", member5);
    }
};

int main()
{
    //Creates a new TestSimpleObject object.
    TestSimpleObject* obj = new TestSimpleObject();

    Console::WriteLine(S"Before serialization the Object* contains: ");
    obj->Print();

    //Opens a file and serializes the object into it in binary format.
    Stream* stream = File::Open(S"data.xml", FileMode::Create);
    SoapFormatter* formatter = new SoapFormatter();

    //BinaryFormatter* formatter = new BinaryFormatter();

    formatter->Serialize(stream, obj);
    stream->Close();

    //Empties obj.
    obj = 0;

    //Opens file S"data.xml" and deserializes the object from it.
    stream = File::Open(S"data.xml", FileMode::Open);
    formatter = new SoapFormatter();
    //formatter = new BinaryFormatter();

    obj = dynamic_cast<TestSimpleObject*>(formatter->Deserialize(stream));
    stream->Close();

    Console::WriteLine(S"");
    Console::WriteLine(S"After deserialization the object contains: ");
    obj->Print();
}

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Namespace: System

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework

Assembly: Mscorlib (in Mscorlib.dll)

See Also

NonSerializedAttribute Members | System Namespace | SerializableAttribute | Attribute | ISerializable | Extending Metadata Using Attributes | XML and SOAP Serialization

Show:
© 2014 Microsoft