Get string property name from expression

2019-02-03 12:16发布

I'm trying to write a strongly typed helper which would be something like this:

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

for now I have this:

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

Anybody knows how to get this ?

4条回答
时光不老,我们不散
2楼-- · 2019-02-03 12:46

Currently using this class when I need this functionality outside of web project where System.Web.Mvc reference shouldn't exist:

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));
    }
  }
}

Good thing about this one is - it does not lose dots when going deeper than just one level.

查看更多
Fickle 薄情
3楼-- · 2019-02-03 12:49

Yet another code.

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

Use ExpressionHelper class. Func is delegate, Expression is generate ExpressionTree at compile time. Expression.Compile() return delegate, but Func don't get ExpressionTree at runtime.

查看更多
做自己的国王
4楼-- · 2019-02-03 13:10
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;
    }
}

You would then call it with:

var propName = Html.Lookup(x => x.FooId);
查看更多
走好不送
5楼-- · 2019-02-03 13:11

a bit late but I am posting a simple solution that's working for me in .Net 4. It has handling for value types on line 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;
    }
查看更多
登录 后发表回答