Gestion de PropertyChanging / PropertyChanged via DynamicProxy de Castle

J’ai actuellement une méthode de définition qui ressemble à ceci:

private ssortingng _a; public virtual ssortingng A { get { return _a; } set { if (_a!= value) { if (this.OnPropertyChanging("A", _a, value)) { var previousValue = _a; _a = value; this.OnPropertyChanged("A", previousValue, value); } } } } 

J’ai implémenté cela avec l’aide de l’apprenti du Dr Wily (http://stackoverflow.com/a/8578507/981225), avec un gestionnaire de changement personnalisé qui garde la trace de la valeur actuelle et ancienne, ainsi que la possibilité de définir le changement. événement comme “annulé”, de sorte que le PropertyChange ne se produira pas.

Cela fonctionne parfaitement. Cependant, nous avons des centaines de propriétés et cela représente beaucoup de code en double.

J’ai déjà utilisé DynamicProxy de Castle pour éviter d’écrire “OnPropertyChanged (” A “)”.

Comment puis-je implémenter la logique dans cet outil de configuration, dans le cadre de la méthode d’interception du proxy? C’est possible? Je vous remercie.

Je suis peut-être un peu en retard, mais je suis tombé sur une tâche similaire dans le modèle Linq-To-SharePoint. J’ai dessiné du code si quelqu’un se pose encore la question.

 using System; using System.Collections.Generic; using System.Linq; using System.Text; using Castle.DynamicProxy; using System.Reflection; using System.ComponentModel; namespace DemoSpace { public abstract class EntityBase : INotifyPropertyChanged, INotifyPropertyChanging { public virtual void OnPropertyChanging(ssortingng propertyName, object value) { if ((null != PropertyChanging)) { PropertyChanging(this, new PropertyChangingEventArgs(propertyName)); } } public virtual void OnPropertyChanged(ssortingng propertyName) { if ((null != PropertyChanged)) { PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); } } public event PropertyChangedEventHandler PropertyChanged; public event PropertyChangingEventHandler PropertyChanging; } public class DemoInterceptor : IInterceptor where T : EntityBase { private T _target; public DemoInterceptor(T target) { _target = target; } public void Intercept(IInvocation invocation) { if (invocation.Method.IsPublic && invocation.Method.Name.StartsWith("set_")) { ssortingng propertyName = invocation.Method.Name.Subssortingng(4); ssortingng privateFieldName = ResolvePropName(propertyName); object original_value = typeof(T).GetField(privateFieldName, BindingFlags.NonPublic | BindingFlags.Instance).GetValue(_target); _target.OnPropertyChanging(propertyName, original_value); invocation.Method.Invoke(_target, invocation.Arguments); _target.OnPropertyChanged(propertyName); } else { invocation.ReturnValue = invocation.Method.Invoke(_target, invocation.Arguments); } } public virtual ssortingng ResolvePropName(ssortingng propertyName) { return "_" + propertyName.Subssortingng(0, 1).ToLower() + propertyName.Subssortingng(1); } } }