I need to add and remove event handlers on my collection change:
public void SetStorageProviderFor<T>(Expression<Func<T, object>> expr, IStorageProvider sp) where T : class
{
MemberExpression me = (MemberExpression)expr.Body;
Set<T>().Local.CollectionChanged +=
(sender, args) =>
{
foreach (var item in args.NewItems)
{
// get entity's property on member expression and set its value
}
}
}
- How do i prevent adding the same handler?
How can i remove some specific handler? I don't think I should repeat all the same code but with "-=" mark...
I also tried to make the handler anAction
to store it in memory and let it be unassigned fromCollectionChanged
at any time:Action<object, NotifyCollectionChangedEventArgs> act = (sender, args) => { }
but CollectionChanged
cannot accept this Action as handler :(
Any help is appreciated.
UPDATE
My apologies, i shoud have posted more real code. The problem is that my handler is using more parameters than only of CollectionChanged
and needs also expr
and sp
of the SetStorageProviderFor
method. In this case I can create named method as Tim S. advices but if i provide it with additional params then i'll not be able to assign it to Set<T>().Local.CollectionChanged
directly, will need anonymous lambda again ;(