There is a class that implements INotifyPropertyChanged, therefore, there is an event in the class
public event PropertyChangedEventHandler PropertyChanged A subscription to this event occurs in more than 300 places with a simple + operator. The goal is to use WeakEventManager to avoid memory leaks. Of course, I don’t want to change the way I subscribe to in 300 places, the poet wants to get out somehow. All I could come up with (but it does not work):
public event PropertyChangedEventHandler PropertyChanged { add { WeakEventManager<BaseNotifyPropertyChanged, EventArgs>.AddHandler(this, "PropertyChanged", (EventHandler<EventArgs>)value); //либо же PropertyChangedEventManager.AddHandler(this, value, "IsDirty"); } remove { //аналогичный код } } And in the case of WeakEventManager, and PropertyChangedEventManager, the compiler curses the impossibility of casting PropertyChangedEventHandler to EventHandler. Is there any way to solve the problem?
UPD:
I solved the problems above in this way:
public event PropertyChangedEventHandler PropertyChanged { add { WeakEventManager<BaseNotifyPropertyChanged, EventArgs>.AddHandler(this, "PropertyChanged", (sender, e) => value(sender, (PropertyChangedEventArgs)e)); } remove { WeakEventManager<BaseNotifyPropertyChanged, EventArgs>.RemoveHandler(this, "PropertyChanged", (sender, e) => value(sender, (PropertyChangedEventArgs)e)); } } But now in this code
protected virtual void RaiseOnPropertyChangedEvent(string propertyName) { var handler = PropertyChanged; if (handler != null) { handler(this, new PropertyChangedEventArgs(propertyName)); } } The compiler swears: The event Property can be seen only on the left hand side of + = or - =
The error text is quite common, but it appears in other people in a different context.