lundi 4 mai 2015

Abstract away property setter with INotifyPropertyChanged implemented?

I have ViewModel base class defined as follows:

public abstract class AbstractViewModel : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    protected void SendPropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}

In derived classes i do as follows:

    SomeType someProperty;    
    public SomeType SomeProperty
    {
        get { return someProperty; }
        set
        {
            if (someProperty != value)
            {
                someProperty = value;
                SendPropertyChanged("SomeProperty");
            }
        }

    }   

I thought, why repeat this logic for every setter, I can define this in method specified in AbstractViewModel! I came up with something like this:

    protected void SetPropertyValue(ref object oldValue, ref object newValue, MethodBase method)
    {
        if (!oldValue.Equals(newValue))
        {
            oldValue = newValue;
            SendPropertyChanged(method.Name);
        }
    }

In the setter of property:

SetPropertyValue(ref someProperty, ref value, MethodBase.GetCurrentMethod());

But I get error:

The best overloaded method match for 'ViewModel.AbstractViewModel.SetPropertyValue(ref object, ref object, System.Reflection.MethodBase)' has some invalid arguments

What I'm doing wrong here?





Aucun commentaire:

Enregistrer un commentaire