[Serializable] // Making your exception serializable
public class MyException : Exception, ISerializable
{
private String m_errorId;
public String ErrorId
{
get { return m_errorId; }
}
public override String Message
{
get
{
if (m_errorId == null)
{
return base.Message;
}
StringBuilder msg = new StringBuilder(base.Message);
msg.AppendFormat(" [Error Id = {0}]{1}", m_errorId, Environment.NewLine);
return msg.ToString();
}
}
public MyException()
: base()
{
}
public MyException(String message)
: base(message)
{
}
public MyException(String message, Exception innerException)
: base(message, innerException)
{
}
public MyException(String message, String errorId)
: this(message)
{
m_errorId = errorId;
}
public MyException(String message, String errorId, Exception innerException)
: this(message, innerException)
{
m_errorId = errorId;
}
// This protected constructor is used for deserialization.
protected MyException(SerializationInfo info, StreamingContext context)
: base(info, context)
{
if (info == null)
{
throw new System.ArgumentNullException("info");
}
m_errorId = info.GetString("ErrorId");
}
#region ISerializable Members
// Describes set of security permissions applied to the code.
[SecurityPermission(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.SerializationFormatter)]
void ISerializable.GetObjectData(SerializationInfo info, StreamingContext context)
{
if (info == null)
{
throw new System.ArgumentNullException("info");
}
info.AddValue("ErrorId", m_errorId);
base.GetObjectData(info, context);
}
#endregion
}