从表达式获取字符串属性名称

时间:2010-09-23 13:00:54

标签: c# .net asp.net-mvc lambda

我正在尝试编写一个强类型的帮助器 这将是这样的:

Html.Lookup(x => x.FooId);

现在我有这个:

public static MvcHtmlString Lookup<T,TReturn>(this HtmlHelper<T> html, Func<T, TReturn> expression)
{
     // get string "FooId" here
}

有人知道如何获得这个吗?

4 个答案:

答案 0 :(得分:24)

public static class ExpressionsExtractor
{
    public static string Lookup<T, TProp>(this HtmlHelper<T> html, Expression<Func<T, TProp>> expression)
    {
        var memberExpression = expression.Body as MemberExpression;

        if (memberExpression == null)
            return null;

        return memberExpression.Member.Name;
    }
}

然后你会用:

来调用它
var propName = Html.Lookup(x => x.FooId);

答案 1 :(得分:14)

又一个代码。

public MvcHtmlString Lookup<T, TReturn>(this HtmlHelper<T> html, Expression<Func<T, TReturn>> expression)
{
  return MvcHtmlString.Create(ExpressionHelper.GetExpressionText(expression));
}

使用ExpressionHelper类。 Func是委托,Expression在编译时生成ExpressionTree。 Expression.Compile()返回委托,但Func在运行时没有得到ExpressionTree。

答案 2 :(得分:8)

当我需要在不存在System.Web.Mvc引用的web项目之外的此功能时,目前正在使用此类:

namespace Interreg.Domain{
  using System;
  using System.Linq.Expressions;
  public class PropertyName{
    public static string For<T>(
      Expression<Func<T,object>> expression){
      var body=expression.Body;
      return GetMemberName(body);
    }
    public static string For(
      Expression<Func<object>> expression){
      var body=expression.Body;
      return GetMemberName(body);
    }
    public static string GetMemberName(
      Expression expression){
      if(expression is MemberExpression){
        var memberExpression=(MemberExpression)expression;
        if(memberExpression.Expression.NodeType==
           ExpressionType.MemberAccess)
          return GetMemberName(memberExpression.Expression)+"."+memberExpression.Member.Name;
        return memberExpression.Member.Name;
      }
      if(expression is UnaryExpression){
        var unaryExpression=(UnaryExpression)expression;
        if(unaryExpression.NodeType!=ExpressionType.Convert)
          throw new Exception(string.Format("Cannot interpret member from {0}",expression));
        return GetMemberName(unaryExpression.Operand);
      }
      throw new Exception(string.Format("Could not determine member from {0}",expression));
    }
  }
}

关于这一点的好处是 - 当深度超过一个级别时它不会丢失点。

答案 3 :(得分:0)

有点晚了但是我发布了一个简单的解决方案,它在.Net 4中为我工作。它在第4行处理了值类型

public PropertyInfo GetPropertyInfo<TSource>(Expression<Func<TSource, object>> propertyLambda) {
        var member = propertyLambda.Body as MemberExpression;
        if (member == null) {// value types return Convert(x.property) which can't be cast to MemberExpression
            var expression = propertyLambda.Body as UnaryExpression;
            member = expression.Operand as MemberExpression;
        }
        return member.Member as PropertyInfo;
    }