在物业变更前处理事件

时间:2015-03-05 05:35:29

标签: c#

是否有一个事件处理程序允许我在类变量值之前执行某些操作?我只知道INotifyPropertyChanged / OnPropertyChanged,但这是在修改了值之后。

2 个答案:

答案 0 :(得分:1)

public class MyClass
{
    public event Action NotifyBeforeChanged;

    private int _value;
    public int Value
    {
        get
        {
            return _value;
        }
        set
        {
            NotifyBeforeChanged();
            _value = value;
        }
    }
}

答案 1 :(得分:1)

INotifyPropertyChanging的示例:

public class AClass : INotifyPropertyChanging
{

    private int aField;

    public int AProperty
    {
        get { return aField; }
        set 
        {
            OnPropertyChanging("AProperty");
            aField = value; 
        }
    }


    private void OnPropertyChanging(string propertyName) 
    {
        PropertyChanging(this, new PropertyChangingEventArgs(propertyName));
    }

    public event PropertyChangingEventHandler PropertyChanging = delegate { };
}