ReadOnlyPropertyException.cs 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. using System;
  2. using System.Reflection;
  3. using System.Runtime.Serialization;
  4. namespace Masuit.Tools.Mapping.Exceptions
  5. {
  6. /// <summary>
  7. /// 只读属性的异常
  8. /// </summary>
  9. [Serializable]
  10. public class ReadOnlyPropertyException : MapperExceptionBase
  11. {
  12. /// <summary>
  13. /// 构造函数
  14. /// </summary>
  15. /// <param name="property">属性</param>
  16. public ReadOnlyPropertyException(PropertyInfo property) : base(ValidateParameter(property))
  17. {
  18. }
  19. /// <summary>
  20. /// 构造函数
  21. /// </summary>
  22. public ReadOnlyPropertyException()
  23. {
  24. }
  25. /// <summary>
  26. /// 构造函数
  27. /// </summary>
  28. /// <param name="serializer">序列化信息</param>
  29. /// <param name="context">上下文</param>
  30. protected ReadOnlyPropertyException(SerializationInfo serializer, StreamingContext context) : base(serializer, context)
  31. {
  32. }
  33. /// <summary>
  34. /// 构造函数
  35. /// </summary>
  36. /// <param name="exceptionMessage">异常信息</param>
  37. /// <param name="innerException">内部异常</param>
  38. public ReadOnlyPropertyException(string exceptionMessage, Exception innerException) : base(exceptionMessage, innerException)
  39. {
  40. }
  41. /// <summary>
  42. /// 构造函数
  43. /// </summary>
  44. /// <param name="exceptionMessage">异常信息</param>
  45. public ReadOnlyPropertyException(string exceptionMessage) : base(exceptionMessage)
  46. {
  47. }
  48. private static string ValidateParameter(PropertyInfo property)
  49. {
  50. return $"目标对象的属性 '{property.Name}' 是只读的";
  51. }
  52. }
  53. }