Saving a Document Using System.SerializableAttribute : Attribute « Attribute « C# / CSharp Tutorial






using System;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;

class Program
{
  public static void Main()
  {
      Stream stream;
      Document documentBefore = new Document();
      documentBefore.Title ="test";
      Document documentAfter;

      using (stream = File.Open(documentBefore.Title + ".bin", FileMode.Create))
      {
          BinaryFormatter formatter = new BinaryFormatter();
          formatter.Serialize(stream, documentBefore);
      }

      using (stream = File.Open(documentBefore.Title + ".bin", FileMode.Open))
      {
          BinaryFormatter formatter = new BinaryFormatter();
          documentAfter = (Document)formatter.Deserialize(stream);
      }

      Console.WriteLine(documentAfter.Title);
  }
}
[Serializable]
class Document
{

  public string Title = null;
  public string Data = null;

  [NonSerialized]
  public long _WindowHandle = 0;

  class Image
  {
  }
  [NonSerialized]
                            private Image Picture = new Image();
}








10.1.Attribute
10.1.1.Use Attributes to mark a method
10.1.2.Assembly-Level Attributes
10.1.3.Custom Attributes
10.1.4.Providing an Attribute Constructor
10.1.5.Retrieving a Specific Attribute and Checking Its Initialization
10.1.6.Saving a Document Using System.SerializableAttribute