| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Runtime.Serialization;
- using System.Security.Permissions;
- using System.Text;
- namespace HslCommunication.BasicFramework
- {
- /****************************************************************************
- *
- * 创建日期: 2017年6月25日 15:45:40
- * 功能: 一个基础的泛型异常类
- * 参考: 参考《CLR Via C#》P413
- *
- ***************************************************************************/
- /// <summary>
- /// 一个自定义的支持序列化反序列化的异常类,具体用法参照第四版《CLR Via C#》P414
- /// </summary>
- /// <typeparam name="TExceptionArgs"></typeparam>
- [Serializable]
- public sealed class Exception<TExceptionArgs> : Exception, ISerializable where TExceptionArgs : ExceptionArgs
- {
- /// <summary>
- /// 用于反序列化的
- /// </summary>
- private const string c_args = "Args";
- private readonly TExceptionArgs m_args;
- /// <summary>
- /// 消息
- /// </summary>
- public TExceptionArgs Args { get { return m_args; } }
- /// <summary>
- /// 实例化一个异常对象
- /// </summary>
- /// <param name="message"></param>
- /// <param name="innerException"></param>
- public Exception(string message = null, Exception innerException = null) : this(null, message, innerException)
- {
- }
- /// <summary>
- /// 实例化一个异常对象
- /// </summary>
- /// <param name="args"></param>
- /// <param name="message"></param>
- /// <param name="innerException"></param>
- public Exception(TExceptionArgs args, string message = null, Exception innerException = null) : base(message, innerException)
- {
- m_args = args;
- }
- /******************************************************************************************************
- *
- * 这个构造器用于反序列化的,由于类是密封的,所以构造器是私有的
- * 如果这个构造器不是密封的,这个构造器就应该是受保护的
- *
- ******************************************************************************************************/
- [SecurityPermission(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.SerializationFormatter)]
- private Exception(SerializationInfo info, StreamingContext context) : base(info, context)
- {
- m_args = (TExceptionArgs)info.GetValue(c_args, typeof(TExceptionArgs));
- }
- /******************************************************************************************************
- *
- * 这个方法用于序列化,由于ISerializable接口的存在,这个方法必须是公开的
- *
- ******************************************************************************************************/
- /// <summary>
- /// 获取存储对象的序列化数据
- /// </summary>
- /// <param name="info"></param>
- /// <param name="context"></param>
- [SecurityPermission(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.SerializationFormatter)]
- public override void GetObjectData(SerializationInfo info, StreamingContext context)
- {
- info.AddValue(c_args, m_args);
- base.GetObjectData(info, context);
- }
- /// <summary>
- /// 获取描述当前异常的消息
- /// </summary>
- public override string Message
- {
- get
- {
- string baseMsg = base.Message;
- return m_args == null ? baseMsg : baseMsg + " (" + m_args.Message + ")";
- }
- }
- /// <summary>
- /// 确定指定的object是否等于当前的object
- /// </summary>
- /// <param name="obj"></param>
- /// <returns></returns>
- public override bool Equals(object obj)
- {
- Exception<TExceptionArgs> other = obj as Exception<TExceptionArgs>;
- if (other == null) return false;
- return object.Equals(m_args, other.m_args) && base.Equals(obj);
- }
- /// <summary>
- /// 用作特定类型的哈希函数
- /// </summary>
- /// <returns></returns>
- public override int GetHashCode()
- {
- return base.GetHashCode();
- }
- }
- /// <summary>
- /// 异常消息基类
- /// </summary>
- [Serializable]
- public abstract class ExceptionArgs
- {
- /// <summary>
- /// 获取消息文本
- /// </summary>
- public virtual string Message
- {
- get { return string.Empty; }
- }
- }
- }
|