当前位置: 代码迷 >> C# >> C#中get与set解决办法
  详细解决方案

C#中get与set解决办法

热度:91   发布时间:2016-05-05 02:37:44.0
C#中get与set

        private string _abase;
        private string _acoating;
        private string _aholding;
        private string _amin;

        public string Abase
        {
            get { return _abase; }
            set
            {
                this._abase = value;
                NotifyPropertyChanged("Abase");
            }
        }
       public string Acoating
        {
            get { return _acoating; }
            set
            {
                this._acoating = value;
                NotifyPropertyChanged("Acoating");
            }
        }
        public string Aholding
        {
            get { return _aholding; }
            set
            {
                this._aholding = value;
                NotifyPropertyChanged("Aholding");
            }
        }
        public string Amin
        {
            get { return _amin; }
            set
            {
                this._amin = value;
                NotifyPropertyChanged("Amin");
            }
        }

能不能把下面这访问器弄成通用的方法,通过传属性名称来实现。有时候属性太多了,在class里代码量就占了大部分,复制也花时间。

public string Abase
        {
            get { return _abase; }
            set
            {
                this._abase = value;
                NotifyPropertyChanged("Abase");
            }
 }

------解决思路----------------------
不要想偷懒。  如果属性改变  你能知道 那个属性变了么。
------解决思路----------------------
c# 6.0开始有nameof关键字了……
------解决思路----------------------
那得用反射,这个效率比较差。如果要求苛刻可以问问Emit方面的大神
http://www.cnblogs.com/mrlen/archive/2010/06/10/1755357.html
------解决思路----------------------
http://dearymz.blog.163.com/blog/static/2056574201231810161814/
------解决思路----------------------
public enum a { base, coating, holding, min }
string[] _ = new string[4];
string Get(a p){ return _[(int)p]; }
void Set(a p, string v){ _[(int)p] = v; }

------解决思路----------------------
安装coderush,然后在继承了INotifyPropertyChanged滴class里输入“ps”然后按空格你会发现惊喜地
------解决思路----------------------
建议看一下VS的代码段功能,自己写个代码段模板,例如我自己的就是输入propn然后Tab,就可以生成代码,只要把变量名字写一下就可以了。跟for+Tab,foreach+Tab效果类似。
------解决思路----------------------
这种东西可以用 T4 模板生成代码,也可以通过 AOP 实现,AOP 分静态和动态两种,静态的是修改编译后的程序集,比如PostSharp,或者Fody之类(这里是Fody用来实现NotifyPropertyChanged的插件)。动态是运行的时候生成新的接口的实现或者子类来拦截方法调用,加入自定义逻辑。这个有老牌的 Castle.DynamicProxy,或者如果你有用 IoC / DI 框架,它们大部分都是支持拦截的。比如 Autofac的拦截功能,它给了一个写log的例子,NotifyPropertyChanged是差不多的。
------解决思路----------------------
文本模板是一种解决方法。

新建项 - 文本模板:类名.tt


<#@ template debug="true" hostspecific="false" language="C#" #>
<#@ assembly name="System.Core" #>
<#@ import namespace="System.Linq" #>
<#@ import namespace="System.Text" #>
<#@ import namespace="System.Collections.Generic" #>
<#@ output extension=".cs" #>

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace 命名空间
{
    partial class 类名
    {
<#
// 属性名称数组。
string[] propertyNames = { "Abase" };
string fieldName;
foreach (var propertyName in propertyNames)
{
    // 属性名称长度不小于 2 个字符。
    fieldName = "_" + propertyName[0] + propertyName.Substring(1);
#>

        public string <#= propertyName #>
        {
            get { return <#= fieldName #>; }
            set
            {
                this.<#= fieldName #> = value;
                NotifyPropertyChanged("<#= propertyName #>");
            }
        }

        private string <#= fieldName #>;

<#
}
#>
    }
}
  相关解决方案