title: OnSerializingAttribute vs ISerializable
body: <p>I was reading custom serialization article on msdn: <a href="http://msdn.microsoft.com/en-us/library/ty01x675%28VS.80%29.aspx" rel="nofollow noreferrer">http://msdn.microsoft.com/en-us/library/ty01x675%28VS.80%29.aspx</a></p>
  
  <p>It mentions that there are two ways of implementing custom serialization:<br />
  1, using <code>OnDeserializedAttribute, OnDeserializingAttribute, OnSerializedAttribute, OnSerializingAttribute</code> <br />
  2, implement <code>ISerializable</code> interface</p>
  
  <p>According to MSDN, method #1 is "best practice and easiest", but I fail to understand how are the two methods even the same thing. To my understanding, the <code>OnSerializing|OnSerialized|etc</code> attributes allow you to hook methods to particular phases of the serialization, while <code>ISerializable</code> interface allows you to directly modify what goes into and comes out of <code>SerializationInfo</code>. Is this correct?</p>
  
  <p>To put my confusion into context, how do you implement OnSerializing to serialize object field under different name? like the following code:</p>
  
  <pre>
  public virtual void GetObjectData(SerializationInfo info, StreamingContext context)
  {
      info.AddValue("i", n1);
      info.AddValue("j", n2);
      info.AddValue("k", str);
  }
  </pre>
  
  <p><strong>Update</strong>: Although Frederik Gheysels's answer is not completely correct (decorating with Serializable is pre-requisite for both OnSerializing/OnDeserializing attributes and ISerializable interface), he did however points out that OnSerializing/OnDeserializing attributes are meant to be supplement of ISerializable, so I am accepting his answer on this point.</p>