防爆pression< Func键>问题

防爆pression< Func键>问题

问题描述:

我希望能简洁明了地说明这一点。我有一个前pression:

I'm hoping to explain this clearly and concisely. I've got an expression:

Expression<Func<TObject, TProperty>> expression

这是我想要得到的属性名。一切都很好,很正常,直到,一个转换前pression遇到(在 getPropertyName方法方式 - 这是我想对这个问题进行排序),即正常的性能可能会出来为 {E =&GT; e.EmployeeID} ,但在少数情况下,我得到 {E =&gt; 0的结果;转换(e.EmployeeID)} 。这实际上意味着,我不能辨别正确的属性名称(我不想解析为例外,如转换()等)。

that I'm trying to get the property name from. All is fine and dandy 'UNTIL', a convert expression is encountered (in the GetPropertyName method - this is where i want to sort the issue) i.e. normal properties may come out as {e =>e.EmployeeID} but in a few cases, I'm getting a result of {e => Convert(e.EmployeeID)}. This in effect means that I can't discern the correct property name (i don't want to parse for exceptions such as Convert() etc).

我怎样才能干净地提取前pression名称作为属性。下面是code我使用,可以随意篡改是:

How can I cleanly extract the expression name as a property. Below is the code I'm using, so feel free to tamper with that:

public static class ExpressionExtensions
{
    public static string GetPropertyName<TObject, TProperty>(
        this Expression<Func<TObject, TProperty>> expression) where TObject : class
    {
        if (expression.Body.NodeType == ExpressionType.Call)
        {
            MethodCallExpression methodCallExpression = (MethodCallExpression)expression.Body;
            string name = ExpressionExtensions.GetPropertyName(methodCallExpression);
            return name.Substring(expression.Parameters[0].Name.Length + 1);
        }
        return expression.Body.ToString().Substring(expression.Parameters[0].Name.Length + 1);
    }

    private static string GetPropertyName(MethodCallExpression expression)
    {
        MethodCallExpression methodCallExpression = expression.Object as MethodCallExpression;
        if (methodCallExpression != null)
        {
            return GetPropertyName(methodCallExpression);
        }
        return expression.Object.ToString();
    }
}

和我调用它像这样:

string propertyName = expression.GetPropertyName();
// which ideally should return a value of EmployeeID or ReportsTo
// as per the usage example below

和这个泡沫到一定arbitary用法如:

and this bubbles up to some arbitary usage such as:

var tree = items2.AsHierarchy(e => e.EmployeeID, e => e.ReportsTo);

希望这给足够的信息来让我得到的绞索!

Hope this gives enough info to get me off the noose!!

欢呼声

您应该有一个转换的节点在你的前pression树,所以你应该测试NODETYPE转换的节点,那么,如果真的去一级铸造到字符串前更深。尝试是这样的:

You should have a "convert" node in your expression tree, so you should test the node for nodetype "convert", then if true go one level deeper before casting to string. Try something like this:

        public static string GetMemberName<TSource,TMember>(this Expression<Func<TSource,TMember>> memberReference)
    {
        MemberExpression expression = memberReference.Body as MemberExpression;
        if (expression == null)
        {
            UnaryExpression convertexp = memberReference.Body as UnaryExpression;
            if(convertexp!=null)
            expression = convertexp.Operand as MemberExpression; ;
        }
        if(expression==null)
            throw new ArgumentNullException("memberReference");

        return expression.Member.Name;
    }