InpcPropertyAccessorPlugin.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. // Copyright (c) The Avalonia Project. All rights reserved.
  2. // Licensed under the MIT license. See licence.md file in the project root for full license information.
  3. using System;
  4. using System.ComponentModel;
  5. using System.Linq;
  6. using System.Reactive.Linq;
  7. using System.Reflection;
  8. using Avalonia.Utilities;
  9. namespace Avalonia.Data.Core.Plugins
  10. {
  11. /// <summary>
  12. /// Reads a property from a standard C# object that optionally supports the
  13. /// <see cref="INotifyPropertyChanged"/> interface.
  14. /// </summary>
  15. public class InpcPropertyAccessorPlugin : IPropertyAccessorPlugin
  16. {
  17. /// <inheritdoc/>
  18. public bool Match(object obj, string propertyName) => true;
  19. /// <summary>
  20. /// Starts monitoring the value of a property on an object.
  21. /// </summary>
  22. /// <param name="reference">The object.</param>
  23. /// <param name="propertyName">The property name.</param>
  24. /// <returns>
  25. /// An <see cref="IPropertyAccessor"/> interface through which future interactions with the
  26. /// property will be made.
  27. /// </returns>
  28. public IPropertyAccessor Start(WeakReference<object> reference, string propertyName)
  29. {
  30. Contract.Requires<ArgumentNullException>(reference != null);
  31. Contract.Requires<ArgumentNullException>(propertyName != null);
  32. reference.TryGetTarget(out object instance);
  33. var p = instance.GetType().GetRuntimeProperties().FirstOrDefault(x => x.Name == propertyName);
  34. if (p != null)
  35. {
  36. return new Accessor(reference, p);
  37. }
  38. else
  39. {
  40. var message = $"Could not find CLR property '{propertyName}' on '{instance}'";
  41. var exception = new MissingMemberException(message);
  42. return new PropertyError(new BindingNotification(exception, BindingErrorType.Error));
  43. }
  44. }
  45. private class Accessor : PropertyAccessorBase, IWeakSubscriber<PropertyChangedEventArgs>
  46. {
  47. private readonly WeakReference<object> _reference;
  48. private readonly PropertyInfo _property;
  49. private bool _eventRaised;
  50. public Accessor(WeakReference<object> reference, PropertyInfo property)
  51. {
  52. Contract.Requires<ArgumentNullException>(reference != null);
  53. Contract.Requires<ArgumentNullException>(property != null);
  54. _reference = reference;
  55. _property = property;
  56. }
  57. public override Type PropertyType => _property.PropertyType;
  58. public override object Value
  59. {
  60. get
  61. {
  62. var o = GetReferenceTarget();
  63. return (o != null) ? _property.GetValue(o) : null;
  64. }
  65. }
  66. public override bool SetValue(object value, BindingPriority priority)
  67. {
  68. if (_property.CanWrite)
  69. {
  70. _eventRaised = false;
  71. _property.SetValue(GetReferenceTarget(), value);
  72. if (!_eventRaised)
  73. {
  74. SendCurrentValue();
  75. }
  76. return true;
  77. }
  78. return false;
  79. }
  80. void IWeakSubscriber<PropertyChangedEventArgs>.OnEvent(object sender, PropertyChangedEventArgs e)
  81. {
  82. if (e.PropertyName == _property.Name || string.IsNullOrEmpty(e.PropertyName))
  83. {
  84. _eventRaised = true;
  85. SendCurrentValue();
  86. }
  87. }
  88. protected override void SubscribeCore()
  89. {
  90. SubscribeToChanges();
  91. SendCurrentValue();
  92. }
  93. protected override void UnsubscribeCore()
  94. {
  95. var inpc = GetReferenceTarget() as INotifyPropertyChanged;
  96. if (inpc != null)
  97. {
  98. WeakSubscriptionManager.Unsubscribe(
  99. inpc,
  100. nameof(inpc.PropertyChanged),
  101. this);
  102. }
  103. }
  104. private object GetReferenceTarget()
  105. {
  106. _reference.TryGetTarget(out object target);
  107. return target;
  108. }
  109. private void SendCurrentValue()
  110. {
  111. try
  112. {
  113. var value = Value;
  114. PublishValue(value);
  115. }
  116. catch { }
  117. }
  118. private void SubscribeToChanges()
  119. {
  120. var inpc = GetReferenceTarget() as INotifyPropertyChanged;
  121. if (inpc != null)
  122. {
  123. WeakSubscriptionManager.Subscribe(
  124. inpc,
  125. nameof(inpc.PropertyChanged),
  126. this);
  127. }
  128. }
  129. }
  130. }
  131. }