1

对于控件和网格的数据绑定对象,我讨厌属性名称是魔术字符串,所以我创建了一个非常简单的方法,如下所示:

public static string GetPropertyName<PropertyType>(Expression<Func<T, PropertyType>> expressionForProperty)
    {
        MemberExpression expression = expressionForProperty.Body as MemberExpression;
        return expression.Member.Name;
    }

这让我可以使用如下代码:

Product.GetPropertyName(m => m.Name)

返回“名称”。

这非常适用于基本对象。但是,如果我将方法调用更改为:

Product.GetPropertyName(m => m.ProductCategory.Name)

这也返回“名称”。但为了使数据绑定工作,我需要它返回“ProductCategory.Name”。有没有办法通过更改方法“GetPropertyName”来解决这个问题?

一个可能的解决方法是这样做:

string test = Product.GetPropertyName(p => p.ProductCategory) + "." + ProductCategory.GetPropertyName(pc => pc.Name)

但是,这不是一个简洁的解决方案。

4

2 回答 2

3

这是我可能在 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;
    }
}
于 2013-07-26T13:45:19.467 回答
0

我想出了以下似乎可行的方法:

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;
}
于 2013-07-29T09:49:03.130 回答