Get string name of property using reflection

前端 未结 7 869
北海茫月
北海茫月 2020-11-28 06:56

There is a whole wealth of reflection examples out there that allow you to get either:

    1. All properties in a class

    2. A single property, provided you kn

相关标签:
7条回答
  • 2020-11-28 07:40

    acutully the right answer which is written by TKTS, and here I just want to convert his syntax to C#

    public static string GetPropertyName<t>(Expression<Func<t>> PropertyExp)
    {
    return (PropertyExp.Body as MemberExpression).Member.Name;
    }
    

    and the usage of this code goes like the example as below:

    string name = GetPropertyName(() => (new Tasks()).Title);
    

    in addition : there is an exception could be happen that when the comes with all properties has null values, so anybody has to take this on his concentration when hes implementing his code

    Thanks TKTS ..

    0 讨论(0)
提交回复
热议问题