ReferenceObject.cs 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. using System;
  2. namespace ComPDFKit.Tool.UndoManger
  3. {
  4. public class ReferenceObject<T>
  5. {
  6. public class ReferenceChange<T>
  7. {
  8. public T oldValue { get; set; }
  9. public T newValue { get; set; }
  10. }
  11. private static event EventHandler<ReferenceChange<T>> Changed;
  12. public T Data { get;private set; }
  13. public ReferenceObject()
  14. {
  15. Changed += ReferenceObject_Changed;
  16. }
  17. ~ReferenceObject()
  18. {
  19. Changed -= ReferenceObject_Changed;
  20. }
  21. private void ReferenceObject_Changed(object sender, ReferenceChange<T> newData)
  22. {
  23. if(Data==null && newData.oldValue==null)
  24. {
  25. Data = newData.newValue;
  26. return;
  27. }
  28. if (Data.Equals(newData.oldValue))
  29. {
  30. Data = newData.newValue;
  31. }
  32. }
  33. public void Initialize(T newData)
  34. {
  35. Data = newData;
  36. }
  37. public void Update(T newData)
  38. {
  39. ReferenceChange<T> changeData = new ReferenceChange<T>()
  40. {
  41. oldValue = Data,
  42. newValue = newData
  43. };
  44. Changed?.Invoke(this, changeData);
  45. }
  46. }
  47. }