È possibile utilizzare Post Sharp per intercettare la richiesta di modifica di un valore di proprietà. Potresti avere tutte le classi di ricerca ereditate da una classe comune con un List<string>
. Quindi creare un attributo aspetto per aggiungere un nome di proprietà a quel dizionario ogni volta che il valore cambia. Quanto segue è solo un esempio, e ha bug:
[Serializable]
public class PropertyChangeAwareAttribute : OnMethodBoundaryAspect
{
public override void OnEntry(MethodExecutionEventArgs eventArgs)
{
if (eventArgs.Method.Name.StartsWith("set_"))
((SearchBagBase)eventArgs.Instance).PropertiesChanged.Add(eventArgs.Method.Name);
base.OnEntry(eventArgs);
}
}
abstract class SearchBagBase
{
public List<string> PropertiesChanged = new List<String>();
}
[PropertyChangeAware]
class RegularSearch : SearchBagBase
{
public String Key { get; set; }
}
con l'uso:
RegularSearch regularSearch = new RegularSearch();
regularSearch.Key = "toys";
regularSearch.PropertiesChanged.ForEach(Console.WriteLine);
fonte
2010-10-17 19:35:46
Nella mia mente, questo è un modo del tutto valido per farlo a meno che non si dispone di importanti problemi di prestazioni. – steinar
Non è sicuro che sia un modo migliore, ma è possibile implementare l'interfaccia INotifyPropertyChanged. –