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

发布于 2024-09-24 11:41:24 字数 302 浏览 1 评论 0原文

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

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

现在我有这个:

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

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

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 ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

淡淡の花香 2024-10-01 11:41:25

目前,当我在不应该存在 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));
    }
  }
}

这个类的优点是 - 当深入到一个以上时,它不会丢失点等级。

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.

人间☆小暴躁 2024-10-01 11:41:25

有点晚了,但我发布了一个在 .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;
    }

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;
    }
北凤男飞 2024-10-01 11:41: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);
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);
陪你搞怪i 2024-10-01 11:41:24

还有一个代码。

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。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文