Overview

Custom Serialization

[Serializable]
public class Person
{
	public int Id { get; set; }
	public string Name { get; set; }
	
	[NonSerialized]
	private bool isDirty = false;
	
	[OnSerializing]
	internal void OnSerializingMethod(StreamingContext context) {	}
	
	[OnSerialized]
	internal void OnSerializedMethod(StreamingContext context) { }
	
	[OnDeserializing]
	internal void OnDeserializingMethod(StreamingContext context) {	}
	
	[OnDeserialized]
	internal void OnDeserializedMethod(StreamingContext context) { }
}
[Serializable]
public class PersonComplex : ISerializable
{
	public int Id { get; set; }
	public string Name { get; set; }
	private bool isDirty = false;

	public PersonComplex() { }
	
	protected PersonComplex(SerializationInfo info, StreamingContext context)
	{
		Id = info.GetInt32("Value1");
		Name = info.GetString("Value2");
		isDirty = info.GetBoolean("Value3");
	}
	
	[SecurityPermission(SecurityAction.Demand, SerializationFormatter = true)]
	public void GetObjectData(SerializationInfo info, StreamingContext context)
	{
		info.AddValue("Value1", Id);
		info.AddValue("Value2", Name);
		info.AddValue("Value3", isDirty);
	}
}

Binary Serialization