Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I've tried to code a class to avoid a method like "RaisePropertyChanged". I know that I can inherit from a class that has that implementation but in some cases I can't. I've tried with a Extension Method but Visual Studio complain.

public static class Extension
{
    public static void RaisePropertyChanged(this INotifyPropertyChanged predicate, string propertyName)
    {
        if (predicate.PropertyChanged != null)
        {
            predicate.PropertyChanged(propertyName, new PropertyChangedEventArgs(propertyName));
        }
    }
}

It said:

"The event 'System.ComponentModel.INotifyPropertyChanged.PropertyChanged' can only appear on the left hand side of += or -="

like image 473
Vitor Canova Avatar asked Feb 07 '11 18:02

Vitor Canova


3 Answers

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).

like image 132
Dan Tao Avatar answered Oct 23 '22 22:10

Dan Tao


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.

like image 32
Reed Copsey Avatar answered Oct 23 '22 22:10

Reed Copsey


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.

like image 33
Marc Gravell Avatar answered Oct 23 '22 21:10

Marc Gravell