Get the property name used in a Lambda Expression in .NET 3.5

后端 未结 3 815
心在旅途
心在旅途 2020-12-03 01:54

I have a problem that has been nagging me for some time now and I can\'t find the answer.

I need to obtain the name of the property that is being referenced in a La

相关标签:
3条回答
  • 2020-12-03 02:25

    I've got a pretty comprehensive answer here.

    In addition to dealing with expressions like x => x.WeirdPropertyName, it can also deal with "extended" expressions such as x => x.WeirdMember.WeirdPropertyName.

    Here's the code from that answer:

    // code adjusted to prevent horizontal overflow
    static string GetFullPropertyName<T, TProperty>
    (Expression<Func<T, TProperty>> exp)
    {
        MemberExpression memberExp;
        if (!TryFindMemberExpression(exp.Body, out memberExp))
            return string.Empty;
    
        var memberNames = new Stack<string>();
        do
        {
            memberNames.Push(memberExp.Member.Name);
        }
        while (TryFindMemberExpression(memberExp.Expression, out memberExp));
    
        return string.Join(".", memberNames.ToArray());
    }
    
    // code adjusted to prevent horizontal overflow
    private static bool TryFindMemberExpression
    (Expression exp, out MemberExpression memberExp)
    {
        memberExp = exp as MemberExpression;
        if (memberExp != null)
        {
            // heyo! that was easy enough
            return true;
        }
    
        // if the compiler created an automatic conversion,
        // it'll look something like...
        // obj => Convert(obj.Property) [e.g., int -> object]
        // OR:
        // obj => ConvertChecked(obj.Property) [e.g., int -> long]
        // ...which are the cases checked in IsConversion
        if (IsConversion(exp) && exp is UnaryExpression)
        {
            memberExp = ((UnaryExpression)exp).Operand as MemberExpression;
            if (memberExp != null)
            {
                return true;
            }
        }
    
        return false;
    }
    
    private static bool IsConversion(Expression exp)
    {
        return (
            exp.NodeType == ExpressionType.Convert ||
            exp.NodeType == ExpressionType.ConvertChecked
        );
    }
    
    0 讨论(0)
  • 2020-12-03 02:25

    The only way I know of getting the string name of a property is via reflection.

    0 讨论(0)
  • 2020-12-03 02:31

    Here you go:

    string GetPropertyName<T>(Expression<Func<T>> property)
    {
        var propertyInfo = (property.Body as MemberExpression).Member as PropertyInfo;
        if (propertyInfo == null)
        {
            throw new ArgumentException("The lambda expression 'property' should point to a valid Property");
        }
        return propertyInfo.Name;
    }
    
    0 讨论(0)
提交回复
热议问题