【发布时间】:2016-01-07 00:06:51
【问题描述】:
经过一些研究,我发现自定义异常应该如下所示:
using System;
using System.Runtime.Serialization;
namespace YourNamespaceHere
{
[Serializable()]
public class YourCustomException : Exception, ISerializable
{
public YourCustomException() : base() { }
public YourCustomException(string message) : base(message) { }
public YourCustomException(string message, System.Exception inner) : base(message, inner) { }
public YourCustomException(SerializationInfo info, StreamingContext context) : base(info, context) { }
}
}
但我有一个小问题。
我希望上面的异常有两个额外的字段,比如int ID 和int ErrorCode。如何添加这两个字段并初始化它们 - 我应该添加一个 new 构造函数,带有这两个参数和消息参数吗?
您还可以帮助我并展示如何为这个具有两个新属性的新类编写序列化方法吗?
谢谢。
【问题讨论】:
-
只需将属性添加到您的类中
-
@singsuyash:我应该添加接受消息和两个整数的 new 构造函数吗?如果有人能用这两个整数属性总结这些事情以及你链接的内容作为我班级的答案,那就太好了。
-
Shetty 为你添加了一个例子