SoftException.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Runtime.Serialization;
  5. using System.Security.Permissions;
  6. using System.Text;
  7. namespace HslCommunication.BasicFramework
  8. {
  9. /****************************************************************************
  10. *
  11. * 创建日期: 2017年6月25日 15:45:40
  12. * 功能: 一个基础的泛型异常类
  13. * 参考: 参考《CLR Via C#》P413
  14. *
  15. ***************************************************************************/
  16. /// <summary>
  17. /// 一个自定义的支持序列化反序列化的异常类,具体用法参照第四版《CLR Via C#》P414
  18. /// </summary>
  19. /// <typeparam name="TExceptionArgs"></typeparam>
  20. [Serializable]
  21. public sealed class Exception<TExceptionArgs> : Exception, ISerializable where TExceptionArgs : ExceptionArgs
  22. {
  23. /// <summary>
  24. /// 用于反序列化的
  25. /// </summary>
  26. private const string c_args = "Args";
  27. private readonly TExceptionArgs m_args;
  28. /// <summary>
  29. /// 消息
  30. /// </summary>
  31. public TExceptionArgs Args { get { return m_args; } }
  32. /// <summary>
  33. /// 实例化一个异常对象
  34. /// </summary>
  35. /// <param name="message"></param>
  36. /// <param name="innerException"></param>
  37. public Exception(string message = null, Exception innerException = null) : this(null, message, innerException)
  38. {
  39. }
  40. /// <summary>
  41. /// 实例化一个异常对象
  42. /// </summary>
  43. /// <param name="args"></param>
  44. /// <param name="message"></param>
  45. /// <param name="innerException"></param>
  46. public Exception(TExceptionArgs args, string message = null, Exception innerException = null) : base(message, innerException)
  47. {
  48. m_args = args;
  49. }
  50. /******************************************************************************************************
  51. *
  52. * 这个构造器用于反序列化的,由于类是密封的,所以构造器是私有的
  53. * 如果这个构造器不是密封的,这个构造器就应该是受保护的
  54. *
  55. ******************************************************************************************************/
  56. [SecurityPermission(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.SerializationFormatter)]
  57. private Exception(SerializationInfo info, StreamingContext context) : base(info, context)
  58. {
  59. m_args = (TExceptionArgs)info.GetValue(c_args, typeof(TExceptionArgs));
  60. }
  61. /******************************************************************************************************
  62. *
  63. * 这个方法用于序列化,由于ISerializable接口的存在,这个方法必须是公开的
  64. *
  65. ******************************************************************************************************/
  66. /// <summary>
  67. /// 获取存储对象的序列化数据
  68. /// </summary>
  69. /// <param name="info"></param>
  70. /// <param name="context"></param>
  71. [SecurityPermission(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.SerializationFormatter)]
  72. public override void GetObjectData(SerializationInfo info, StreamingContext context)
  73. {
  74. info.AddValue(c_args, m_args);
  75. base.GetObjectData(info, context);
  76. }
  77. /// <summary>
  78. /// 获取描述当前异常的消息
  79. /// </summary>
  80. public override string Message
  81. {
  82. get
  83. {
  84. string baseMsg = base.Message;
  85. return m_args == null ? baseMsg : baseMsg + " (" + m_args.Message + ")";
  86. }
  87. }
  88. /// <summary>
  89. /// 确定指定的object是否等于当前的object
  90. /// </summary>
  91. /// <param name="obj"></param>
  92. /// <returns></returns>
  93. public override bool Equals(object obj)
  94. {
  95. Exception<TExceptionArgs> other = obj as Exception<TExceptionArgs>;
  96. if (other == null) return false;
  97. return object.Equals(m_args, other.m_args) && base.Equals(obj);
  98. }
  99. /// <summary>
  100. /// 用作特定类型的哈希函数
  101. /// </summary>
  102. /// <returns></returns>
  103. public override int GetHashCode()
  104. {
  105. return base.GetHashCode();
  106. }
  107. }
  108. /// <summary>
  109. /// 异常消息基类
  110. /// </summary>
  111. [Serializable]
  112. public abstract class ExceptionArgs
  113. {
  114. /// <summary>
  115. /// 获取消息文本
  116. /// </summary>
  117. public virtual string Message
  118. {
  119. get { return string.Empty; }
  120. }
  121. }
  122. }