从拉姆达前pression检索属性名称

从拉姆达前pression检索属性名称

本文介绍了从拉姆达前pression检索属性名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有更好的方式来获得通过的lambda前pression通过的时候,属性名?
以下是我目前有。

如。

  GetSortingInfo<使用者>(U => u.UserId);

它的工作由浇铸因为只有当财产是一个字符串memberex pression。因为不是所有的属性都是字符串,我不得不使用对象,但随后会返回一个unaryex pression那些。

 公共静态RouteValueDictionary的GetInfo< T>(这个HTML的HtmlHelper,
    防爆pression<&Func键LT; T,对象>>动作),其中T:类
{
    VAR前pression = GetMemberInfo(动作);
    字符串名称=前pression.Member.Name;    返回的GetInfo(HTML,名);
}私有静态MemberEx pression GetMemberInfo(前pression法)
{
    LambdaEx pression波长=方法LambdaEx pression;
    如果(拉姆达== NULL)
        抛出新的ArgumentNullException(办法);    MemberEx pression memberExpr = NULL;    如果(lambda.Body.NodeType ==前pressionType.Convert)
    {
        memberExpr =
            ((UnaryEx pression)lambda.Body).Operand作为MemberEx pression;
    }
    否则,如果(lambda.Body.NodeType ==前pressionType.MemberAccess)
    {
        memberExpr = lambda.Body作为MemberEx pression;
    }    如果(memberExpr == NULL)
        抛出新的ArgumentException(办法);    返回memberExpr;
}


解决方案

我找到了另一种方式,你可以做到这一点是有来源和性质强类型,并明确推断拉姆达的投入。不知道这是正确的术语,但这里是结果。

 公共静态RouteValueDictionary的GetInfo< T,P>(此的HtmlHelper HTML,防爆pression< Func键< T,P>>动作),其中T:类
{
    VAR前pression =(MemberEx pression)action.Body;
    字符串名称=前pression.Member.Name;    返回的GetInfo(HTML,名);
}

然后调用它像这样。

 的GetInfo((用户U)=> u.UserId);

和瞧它的作品。结果
感谢所有。

Is there a better way to get the Property name when passed in via a lambda expression?Here is what i currently have.

eg.

GetSortingInfo<User>(u => u.UserId);

It worked by casting it as a memberexpression only when the property was a string. because not all properties are strings i had to use object but then it would return a unaryexpression for those.

public static RouteValueDictionary GetInfo<T>(this HtmlHelper html,
    Expression<Func<T, object>> action) where T : class
{
    var expression = GetMemberInfo(action);
    string name = expression.Member.Name;

    return GetInfo(html, name);
}

private static MemberExpression GetMemberInfo(Expression method)
{
    LambdaExpression lambda = method as LambdaExpression;
    if (lambda == null)
        throw new ArgumentNullException("method");

    MemberExpression memberExpr = null;

    if (lambda.Body.NodeType == ExpressionType.Convert)
    {
        memberExpr =
            ((UnaryExpression)lambda.Body).Operand as MemberExpression;
    }
    else if (lambda.Body.NodeType == ExpressionType.MemberAccess)
    {
        memberExpr = lambda.Body as MemberExpression;
    }

    if (memberExpr == null)
        throw new ArgumentException("method");

    return memberExpr;
}
解决方案

I found another way you can do it was to have the source and property strongly typed and explicitly infer the input for the lambda. Not sure if that is correct terminology but here is the result.

public static RouteValueDictionary GetInfo<T,P>(this HtmlHelper html, Expression<Func<T, P>> action) where T : class
{
    var expression = (MemberExpression)action.Body;
    string name = expression.Member.Name;

    return GetInfo(html, name);
}

And then call it like so.

GetInfo((User u) => u.UserId);

and voila it works.
Thanks all.

这篇关于从拉姆达前pression检索属性名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 19:42