如何从C#(2.0)中的属性获取属性的名称

如何从C#(2.0)中的属性获取属性的名称,第1张

概述我知道我可以有一个属性,但这比我想去的工作更多……而且不够通用. 我想做点什么 class Whotsit{ private string testProp = "thingy"; public string TestProp { get { return testProp; } set { testProp = value; } 我知道我可以有一个属性,但这比我想去的工作更多……而且不够通用.

我想做点什么

class Whotsit{    private string testProp = "thingy";    public string TestProp     {        get { return testProp; }        set { testProp = value; }    }}...Whotsit whotsit = new Whotsit();string value = Getname(whotsit.TestProp); //precise Syntax up for grabs..

在哪里我期望价值等于“TestProp”

但我不能为我的生活找到正确的反射方法来编写Getname方法…

编辑:我为什么要这样做?我有一个类来存储从’name’,’value’表中读取的设置.这由基于反射的通用方法填充.我很想反写…

/// <summary>/// Populates an object from a datatable where the rows have columns called nameFIEld and ValueFIEld. /// If the property with the 'name' exists,and is not read-only,it is populated from the /// valueFIEld. Any other columns in the datatable are ignored. If there is no property called/// nameFIEld it is ignored. Any propertIEs of the object not found in the data table retain their/// original values./// </summary>/// <typeparam name="T">Type of the object to be populated.</typeparam>/// <param name="toBePopulated">The object to be populated</param>/// <param name="datatable">'name,'value' Data table to populate the object from.</param>/// <param name="nameFIEld">FIEld name of the 'name' fIEld'.</param>/// <param name="valueFIEld">FIEld name of the 'value' fIEld.</param>/// <param name="options">Setting to control conversions - e.g. nulls as empty strings.</param>public static voID PopulateFromnameValueDatatable<T>        (T toBePopulated,System.Data.Datatable datatable,string nameFIEld,string valueFIEld,PopulateOptions options)    {        Type type = typeof(T);        bool nullStringsAsEmptyString = options == PopulateOptions.NullStringsAsEmptyString;        foreach (DaTarow daTarow in datatable.Rows)        {            string name = daTarow[nameFIEld].ToString();            System.Reflection.PropertyInfo property = type.GetProperty(name);            object value = daTarow[valueFIEld];            if (property != null)            {                Type propertyType = property.PropertyType;                if (nullStringsAsEmptyString && (propertyType == typeof(String)))                {                    value = TypeHelper.EmptyStringIfNull(value);                }                else                {                    value = TypeHelper.DefaultIfNull(value,propertyType);                }                property.SetValue(toBePopulated,System.Convert.ChangeType(value,propertyType),null);            }        }    }

进一步编辑:我只是在代码中,有一个Whotsit实例,我想得到’TestProp’属性的文本字符串.我知道这似乎有点奇怪,我可以使用文字“TestProp” – 或者在我的类的情况下使用数据表函数我将在PropertyInfos的foreach循环中.我只是好奇而已…

原始代码有字符串常量,我发现它很笨拙.

解决方法 不,没有什么可以做的.表达式whotsit.TestProp将评估该属性.你想要的是神话般的“infoof”运算符:
// I wish...MemberInfo member = infoof(whotsit.TestProp);

实际上,您只能使用反射来按名称获取属性 – 而不是代码. (当然,或者获取所有属性.虽然它仍然无法帮助您处理样本.)

一种替代方法是使用表达式树:

Expression<Func<string>> = () => whotsit.TestProp;

然后检查表达式树以获取属性.

如果这些都没有帮助,也许你可以告诉我们更多你想要这个功能的原因?

总结

以上是内存溢出为你收集整理的如何从C#(2.0)中的属性获取属性的名称全部内容,希望文章能够帮你解决如何从C#(2.0)中的属性获取属性的名称所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/langs/1249875.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-06-07
下一篇2022-06-07

发表评论

登录后才能评论

评论列表(0条)

    保存