using System;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
public class Starter {
public static void Main(string[] args) {
BinaryFormatter binary = new BinaryFormatter();
FileStream file = new FileStream("data.bin", FileMode.OpenOrCreate);
MyClass obj = new MyClass(5);
binary.Serialize(file, obj);
MyClass obj1 = (MyClass)
binary.Deserialize(file);
Console.WriteLine(obj1.GetValue());
}
}
[Serializable]
public class MyClass {
public MyClass(T init) {
fielda = init;
}
public void GetObjectData(SerializationInfo info,StreamingContext ctx) {
info.AddValue("fielda", fielda, typeof(T));
}
private MyClass(SerializationInfo info,StreamingContext ctx) {
fielda = (T)info.GetValue("fielda", typeof(T));
}
public void SetValue(T data) {
fielda = data;
}
public T GetValue() {
return fielda;
}
private T fielda = default(T);
}