读书人

要对一个Exception进行继承怎么写参

发布时间: 2013-01-17 10:28:55 作者: rapoo

要对一个Exception进行继承,如何写参数为type的方法,有示例代码 谢谢


public class ExceptionUndefinedEnumConditions : Exception
{
public ExceptionUndefinedEnumConditions(Type type)
{
if (type != null)
{
base.Message = type.ToString();
}
else
{
this.Message = "Type Undefined";
}

}
}

我要自定义一个错误,然后传一个Type进来,但我上面的定法不行
应该如何写?
谢谢
[解决办法]
你现在什么问题 base.Message 是只读的,不能赋值
[解决办法]

 public class ExceptionUndefinedEnumConditions : Exception
{
public new string Message
{
get;
set;
}

public ExceptionUndefinedEnumConditions(Type type)
{
if (type != null)
{
this.Message = type.ToString();
}
else
{
this.Message = "Type Undefined";
}
}
}

[解决办法]
如果你需要保留基类的消息,你可以再定义一个属性嘛。

读书人网 >C#

热点推荐