Getting sub property names strongly typed

前端 未结 2 1220
自闭症患者
自闭症患者 2021-01-16 06:40

With databinding objects to controls and grids I hated how the property names would be magic strings, so I created a very simple method as follows:

public st         


        
相关标签:
2条回答
  • 2021-01-16 06:51

    This is a modified version of something I might have found here on StackOVerflow:

    public static class GetPropertyNameExtension
    {
        public static string GetPropertyName<TArg, TProperty>(this Expression<Func<TArg, TProperty>> propertyExpression)
        {
            return propertyExpression.Body.GetMemberExpression().GetPropertyName();
        }
    
        public static string GetPropertyName<TProperty>(this Expression<Func<TProperty>> propertyExpression)
        {
            return propertyExpression.Body.GetMemberExpression().GetPropertyName();
        }
    
        public static string GetPropertyName(this MemberExpression memberExpression)
        {
            if (memberExpression == null)
            {
                return null;
            }
    
            if (memberExpression.Member.MemberType != MemberTypes.Property)
            {
                return null;
            }
    
            var child = memberExpression.Member.Name;
            var parent = GetPropertyName(memberExpression.Expression.GetMemberExpression());
    
            return parent == null ?
                child
                : parent + "." + child;
        }
    
        public static MemberExpression GetMemberExpression(this Expression expression)
        {
            if (expression is MemberExpression)
                return (MemberExpression)expression;
    
            if (expression is UnaryExpression)
                return (MemberExpression)((UnaryExpression)expression).Operand;
    
            return null;
        }
    }
    
    0 讨论(0)
  • 2021-01-16 07:07

    I came up with the following which seems to work:

    public static string GetComplexPropertyName<PropertyType>(Expression<Func<T, PropertyType>> expressionForProperty)
    {
        // get the expression body
        Expression expressionBody = expressionForProperty.Body as MemberExpression;
    
        string expressionAsString = null;
    
        // all properties bar the root property will be "convert"
        switch (expressionBody.NodeType)
        {
            case ExpressionType.Convert:
            case ExpressionType.ConvertChecked:
    
                UnaryExpression unaryExpression = expressionBody as UnaryExpression;
    
                if (unaryExpression != null)
                {
                    expressionAsString = unaryExpression.Operand.ToString();
                }
    
                break;
            default:
                expressionAsString = expressionBody.ToString();
                break;
        }
    
        // makes ure we have got an expression
        if (!String.IsNullOrWhiteSpace(expressionAsString))
        {
            // we want to get rid of the first operand as it will be the root type, so get the first occurence of "."
            int positionOfFirstDot = expressionAsString.IndexOf('.');
    
            if (positionOfFirstDot != -1)
            {
                return expressionAsString.Substring(positionOfFirstDot + 1, expressionAsString.Length - 1 - positionOfFirstDot);
            }
        }
    
        return string.Empty;
    }
    
    0 讨论(0)
提交回复
热议问题