使用属性的... INotifyPropertyChanged的 [英] Use of Attributes... INotifyPropertyChanged

查看:159
本文介绍了使用属性的... INotifyPropertyChanged的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这只是我的想法,因为我是学习上的属性,我用的是INotifyPropertyChanged的太多,只是和想法,我想听到关于它的一些opinios。(我知道这将需要对一些工作编译器而不是在cosumer侧)

This is just something I was thinking as I was learning on Attributes and I was using the INotifyPropertyChanged too much, is just and Idea and I would like to hear some opinios about it.( I know this would require some work on the compiler and not on the cosumer side)

由于INotifyPropertyChanged的是使用相同的图案大部分时间..就像调用火UPS与属性的名称时,可以将其设计成的方法和属性,并使用自动属性?这样编译器知道它需要调用添加到PropertyChanged事件? 因此,如果我们有一流的......

Since INotifyPropertyChanged is used with the same Pattern most of the time .. just like calling the method that fire ups the event with the name of the property ,could it be designed as and Attribute and using Auto-Properties? So that the compiler knows it need to add the call to the PropertyChanged event? So if we have the class....

public class DemoCustomer : INotifyPropertyChanged
{
  public event PropertyChangedEventHandler PropertyChanged;

    private void NotifyPropertyChanged(String info)
    {
    	if (PropertyChanged != null)
    	{
    		PropertyChanged(this, new PropertyChangedEventArgs(info));
    	}
    }

  private string companyNameValue = String.Empty;
         ...
}

而不是声明属性的

Instead of declaring the property

public string CompanyName
    {
    	get
    	{
    		return this.companyNameValue;
    	}

    	set
    	{
                       if (value != this.companyNameValue)
                       {
                          this.companyNameValue = value;
                          NotifyPropertyChanged("CompanyName");
                       }
    	}
    }

我们可以做这样的事情,如果我们可以通过这个属性,它需要生成一个调用的PropertyChanged与属性的名称,如果新的值是从previous不同的指示编译器

we could do something like this if we can indicate to the compiler by this attribute that it needs to generate a call to the PropertyChanged with the name of the property if the new value is different from the previous

[NotifyPropertyChanged]
public string CompanyName
{
  get;set;
}

我们仍然可以保持旧的方式对一些特定行为的编码不使用属性时..

We could still keep coding in the old way for some custom behaviours when no using the Attribute..

推荐答案

您可以<一href="http://www.postsharp.org/forum/postsharp-core/new-plugin-for-inotifypropertychanged-propfu-t507.html">do这与 PostSharp 的,但我不认为这将是在编译内核的任何时间很快。

You can do this with PostSharp, but I don't think it'll be in the core compiler any time soon.

这篇关于使用属性的... INotifyPropertyChanged的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆