Get string name of property using reflection

前端 未结 7 880
北海茫月
北海茫月 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:22

    With C# 6.0 (Visual Studio 2015), you can now use the nameof operator, like this:

    var obj = new MyObject();
    string propertyName = nameof(obj.Property);
    string methodName = nameof(obj.Method);
    string directPropertyName = nameof(MyObject.Property);
    string directMethodName = nameof(MyObject.Method);
    
    0 讨论(0)
  • 2020-11-28 07:27

    In case anyone needs it...here is the VB .NET version of the answer:

    Public Shared Function GetPropertyName(Of t)(ByVal PropertyExp As Expression(Of Func(Of t))) As String
       Return TryCast(PropertyExp.Body, MemberExpression).Member.Name
    End Function
    

    Usage:

    Dim name As String = GetPropertyName(Function() (New myObject).AProperty)
    
    0 讨论(0)
  • 2020-11-28 07:32

    I used the version provided by Jacob but sometimes it gave an exception. It was because the cast was invalid. This version solved the issue:

        public static string GetPropertyName<T>(this Expression<Func<T>> propertyExpression)
        {
            ConstantExpression cExpr = propertyExpression.Body as ConstantExpression;
            MemberExpression mExpr = propertyExpression.Body as MemberExpression;
    
            if (cExpr != null)
                return cExpr.Value.ToString();
            else if (mExpr != null)
                return mExpr.Member.Name;
    
            else return null;
        }
    
    0 讨论(0)
  • 2020-11-28 07:37

    If you already have a PropertyInfo, then @dtb's answer is the right one. If, however, you're wanting to find out which property's code you're currently in, you'll have to traverse the current call stack to find out which method you're currently executing and derive the property name from there.

    var stackTrace = new StackTrace();
    var frames = stackTrace.GetFrames();
    var thisFrame = frames[0];
    var method = thisFrame.GetMethod();
    var methodName = method.Name; // Should be get_* or set_*
    var propertyName = method.Name.Substring(4);
    

    Edit:

    After your clarification, I'm wondering if what you're wanting to do is get the name of a property from a property expression. If so, you might want to write a method like this:

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

    To use it, you'd write something like this:

    var propertyName = GetPropertyName(
        () => myObject.AProperty); // returns "AProperty"
    
    0 讨论(0)
  • 2020-11-28 07:39

    myClassInstance.GetType().GetProperties() gives you PropertyInfo instances for all public properties for myClassInstance's type. (See MSDN for documentation and other overloads.) ´PropertyInfo.Name´ is the actual name of the property. In case you already know the name of the property use GetProperty(name) to retrieve its PropertyInfo object (see MSDN again).

    0 讨论(0)
  • 2020-11-28 07:40

    PropertyInfo.Name

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