Thursday, October 07, 2004

VJs Tip Of The Day - October 7th 2004

Serializing Exceptions for Special Purposes

We should be aware that Exception class implements ISerializable interface for its serialzing needs. If we derive our own exception from ApplicationException and we anticipate chances of our exceptions to be marshalled across process boundaries we need to add [Serializable] attribute to our custom exception... Along with that we might have to implement ISerializable which can be done in the following way:

Have this special contructor and add each private member variable that you want to persist

protected customException(SerializationInfo info, StreamingContext context):base(info,context)
{
_myProperty = info.GetString("_myProperty")
}

Override the GetObjectData method

public override void GetObjectData(SerializationInfo info, Streamingcontext context)
{
info.AddValue("_myProperty", _myProperty)
base.getObjectData(info, context)
}

PS: While browsing through the Microsoft site I found that MS is offering a free skill assesment for .Net (usually worth $40) to everyone... Click Here for details

No comments: