在不添加私有变量的情况下引发propertychanged事件

Jos*_*ech 8 c#

我有一个看起来像这样的房产.

public int NumberOfElephants { get; set; }
Run Code Online (Sandbox Code Playgroud)

此属性在一个observablecollection中,它必须通知另一个属性它已更改.

我该怎么办?

public int NumberOfElephants { get; set { OnPropertyChanged("totalAnimals"); }
Run Code Online (Sandbox Code Playgroud)

没有代码需要像这样

private int _numberOfElephants;
public int NumberOfElephants { 
    get { 
        return _numberOfElephants; 
    } 

    set { 
        _numberOfElephants = value; 
        OnPropertyChanged("totalAnimals"); 
    } 
}
Run Code Online (Sandbox Code Playgroud)

Jon*_*eet 8

你没有.你不能.

自动实现的属性在属性微不足道时才起作用 - 当get/set超出"返回变量的值"或"设置变量的值"时,不需要代码.你可以通过重新格式化来缩短它...当然......我写的是:

private int numberOfElephants;
public int NumberOfElephants {
    get { return numberOfElephants; }

    set {
        _numberOfElephants = value; 
        OnPropertyChanged("totalAnimals"); 
    } 
}
Run Code Online (Sandbox Code Playgroud)

实际上,我会在套装的开始和房产的开始使用"自行打开支架",但我保留了你喜欢的款式.但是在单行上使用"单个表达式get/ set实现"可以使具有大量属性的类更加清晰.


jef*_*ora 6

作为 Jon 答案的替代方案,您可以获得通过 IL weaving 执行此操作的工具,例如 NotifyPropertyWeaver,也可以通过VS Gallery作为工具使用

对于您的示例,根据他们在 Attributes上的doco,您应该能够获得以下内容:

[NotifyProperty(AlsoNotifyFor = new[] { "TotalAnimals" })]
public int NumberOfElephants { get; set; }

public int TotalAnimals { get; set; }
Run Code Online (Sandbox Code Playgroud)

但是,根据他们网站上的以下示例,可能不需要,具体取决于 的实现TotalAnimals

你的代码

public class Person : INotifyPropertyChanged {
    public event PropertyChangedEventHandler PropertyChanged;

    public string GivenNames { get; set; }
    public string FamilyName { get; set; }

    public string FullName
    {
        get
        {
            return string.Format("{0} {1}", GivenNames, FamilyName);
        }
    }

}
Run Code Online (Sandbox Code Playgroud)

什么被编译

public class Person : INotifyPropertyChanged {

    public event PropertyChangedEventHandler PropertyChanged;

    string givenNames;
    public string GivenNames
    {
        get { return givenNames; }
        set
        {
            if (value != givenNames)
            {
                givenNames = value;
                OnPropertyChanged("GivenNames");
                OnPropertyChanged("FullName");
            }
        }
    }

    string familyName;
    public string FamilyName
    {
        get { return familyName; }
        set 
        {
            if (value != familyName)
            {
                familyName = value;
                OnPropertyChanged("FamilyName");
                OnPropertyChanged("FullName");
            }
        }
    }

    public string FullName
    {
        get
        {
            return string.Format("{0} {1}", GivenNames, FamilyName);
        }
    }

    public virtual void OnPropertyChanged(string propertyName)
    {
        var propertyChanged = PropertyChanged;
        if (propertyChanged != null)
        {
            propertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}
Run Code Online (Sandbox Code Playgroud)