我想定义一个具有两个特殊属性的自定义异常:Field和FieldValue,我想要从异常构造函数中的这两个值构建消息.不幸的是,Message是只读的.
这是我所拥有的,但它仍然需要通过消息.
public class FieldFormatException: FormatException { private Fields _field; private string _fieldValue; public Fields Field{ get{ return _field; } } public string FieldValue { get { return _value; } } public FieldFormatException() : base() { } private FieldFormatException(string message) { } public FieldFormatException(string message,Fields field,string value): base(message) { _fieldValue = value; _field = field; } public FieldFormatException(string message,Exception inner,string value): base(message,inner) { _fieldValue = value; _field = field; } protected FieldFormatException(System.Runtime.Serialization.SerializationInfo info,System.Runtime.Serialization.StreamingContext context): base(info,context){} }
解决方法
不知道我是否明白你的问题,但是呢?
public FieldFormatException(Fields field,string value): base(BuildMessage(field,value)) { } private static string BuildMessage(Fields field,string value) { // return the message you want }