BaseProperty.cs 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics.CodeAnalysis;
  4. using System.Runtime.CompilerServices;
  5. using System.Text;
  6. namespace Veldrid.Common
  7. {
  8. public abstract class BaseProperty:IDisposable
  9. {
  10. private bool disposedValue;
  11. public event EventHandler<String> PropertyChanged;
  12. protected void Set<T>(ref T field, T value, [CallerMemberName] String propertyName = "")
  13. {
  14. if(typeof(T).IsValueType && field.ToString()!=value.ToString())
  15. {
  16. field = value;
  17. OnPropertyChanged(this, propertyName);
  18. }
  19. else if (!Object.Equals(field, value))
  20. {
  21. field = value;
  22. OnPropertyChanged(this, propertyName);
  23. }
  24. }
  25. protected void OnPropertyChanged(object sender, string name) => PropertyChanged?.Invoke(sender, name);
  26. protected virtual void Dispose(bool disposing)
  27. {
  28. if (!disposedValue)
  29. {
  30. if (disposing)
  31. {
  32. // TODO: 释放托管状态(托管对象)
  33. }
  34. // TODO: 释放未托管的资源(未托管的对象)并重写终结器
  35. // TODO: 将大型字段设置为 null
  36. disposedValue = true;
  37. }
  38. }
  39. // // TODO: 仅当“Dispose(bool disposing)”拥有用于释放未托管资源的代码时才替代终结器
  40. // ~BaseProperty()
  41. // {
  42. // // 不要更改此代码。请将清理代码放入“Dispose(bool disposing)”方法中
  43. // Dispose(disposing: false);
  44. // }
  45. public void Dispose()
  46. {
  47. // 不要更改此代码。请将清理代码放入“Dispose(bool disposing)”方法中
  48. Dispose(disposing: true);
  49. GC.SuppressFinalize(this);
  50. }
  51. }
  52. }