Why can't I invoke PropertyChanged event from an Extension Method? Why can't I invoke PropertyChanged event from an Extension Method? wpf wpf

Why can't I invoke PropertyChanged event from an Extension Method?


Reed is right. However, I see what you're trying to do (make your code reusable—good for you); and I'll just point out that this is often easily rectified by accepting the PropertyChangedEventHandler delegate itself and passing it from within the INotifyPropertyChanged implementation:

public static void Raise(this PropertyChangedEventHandler handler, object sender, string propertyName){    if (handler != null)    {        handler(sender, new PropertyChangedEventArgs(propertyName));    }}

Then from within your class which implements INotifyPropertyChanged, you can call this extension method like so:

PropertyChanged.Raise(this, "MyProperty");

This works because, as Marc said, within the class declaring the event you can access it like a field (which means you can pass it as a delegate argument to a method, including extension methods).


You can only raise an event from within the class in which it's defined.

This line:

predicate.PropertyChanged(propertyName, new PropertyChangedEventArgs(propertyName));

Will fail, since predicate is not the class which defines this extension method.

This is partly why this is typically handled via a base class instead of via using Extension methods.


Events are really just an API with add/remove (technically there is an optional "invoke" available in the CLI, but the c# compiler doesn't provide it).

What you have there is a field-like event; field-like events act as the add/remove API from outside the declaring type, and act like the field only inside the declaring type, and only when it is necessary to treat as a delegate - most commonly: invoking the subscribers (this is a subtle change here in c# 4; before c# 4 all access from inside the type acts against the field, including +=/-=).

An extension method by definition cannot be inside the declaring type - only top-level (non-nested) static classes can provide extension methods; so no extension method can ever have the direct ability to invoke a field-like event.