ParameterBase.cs 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. using System;
  2. using System.Runtime.Serialization;
  3. namespace WingCloudServer.GeneralDocTools.Model.Parameters
  4. {
  5. /// <summary>
  6. /// parameter base type
  7. /// </summary>
  8. /// <typeparam name="T"></typeparam>
  9. [DataContract]
  10. public abstract class ParameterBase<T> : IParameter
  11. {
  12. private T _initialValue;
  13. private T _value;
  14. /// <summary>
  15. /// The value saved in setting file.Some times the value will be set but not saved to setting file.
  16. /// </summary>
  17. public T SavedValue { get; private set; }
  18. /// <summary>
  19. /// parameter value
  20. /// </summary>
  21. [DataMember]
  22. public T Value
  23. {
  24. get
  25. {
  26. return _value;
  27. }
  28. set
  29. {
  30. if (!Equals(_value, value))
  31. {
  32. _value = value;
  33. OnValueChanged();
  34. }
  35. }
  36. }
  37. /// <summary>
  38. /// parameter name
  39. /// </summary>
  40. public string Name { get; set; } = string.Empty;
  41. /// <summary>
  42. /// parameter section
  43. /// </summary>
  44. public string Section { get; set; } = string.Empty;
  45. /// <summary>
  46. /// parameter value changed state
  47. /// </summary>
  48. public bool IsChanged => !Equals(_initialValue, Value);
  49. /// <summary>
  50. /// value changed event
  51. /// </summary>
  52. public event EventHandler ValueChanged;
  53. /// <summary>
  54. /// notify value changed
  55. /// </summary>
  56. private void OnValueChanged()
  57. {
  58. ValueChanged?.Invoke(this, EventArgs.Empty);
  59. }
  60. /// <summary>
  61. /// change initial value
  62. /// </summary>
  63. public void ChangeInitialValue()
  64. {
  65. _initialValue = Value;
  66. SavedValue = Value;
  67. }
  68. public void ChangeSavedValue()
  69. {
  70. SavedValue = Value;
  71. }
  72. public void ChangeParameterValue(object value)
  73. {
  74. var newParameter = value as ParameterBase<T>;
  75. if (newParameter != null)
  76. {
  77. Value = newParameter.Value;
  78. SavedValue = Value;
  79. }
  80. }
  81. }
  82. }