c# – 带属性的自定义异常
发布时间:2020-12-15 08:31:44 所属栏目:百科 来源:网络整理
导读:经过一些研究后,我发现自定义异常应如下所示: using System;using System.Runtime.Serialization;namespace YourNamespaceHere{ [Serializable()] public class YourCustomException : Exception,ISerializable { public YourCustomException() : base() {
经过一些研究后,我发现自定义异常应如下所示:
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.如何添加这两个字段并初始化它们 – 我应该添加一个新的构造函数,这两个参数和消息参数? 你也可以帮助我并展示如何为这个具有两个新属性的新类编写序列化方法吗? 谢谢. 解决方法
它看起来像这样.
你在这里寻找更多细节 What is the correct way to make a custom .NET Exception serializable? [Serializable()] public class YourCustomException : Exception,ISerializable { public Int Id { get; set; } public Int ErrorCode { get; set; } public YourCustomException() : base() { } public YourCustomException(string message) : base(message) { } public YourCustomException(string message,inner) { } public YourCustomException(SerializationInfo info,context) { } public YourCustomException(string message,int Id,int ErrorCode) : base(message) { this.Id = Id; this.ErrorCode = ErrorCode; } } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |