使用 lambda 表达式调用构建方法

发布于 2024-12-10 05:19:30 字数 1165 浏览 0 评论 0原文

我正在构建一个方法来从 System.ComponentModel.DataAnnotations 获取 DisplayAttribute 以显示在属性的标签上。

[Display(Name="First Name")]
public string FirstName { get; set; }

该方法运行良好:

string GetDisplay(Type dataType, string property)
{
    PropertyInfo propInfo = dataType.GetProperty(property);
    DisplayAttribute attr = propInfo.GetCustomAttributes(false).OfType<DisplayAttribute>().FirstOrDefault();

    return (attr == null) ? propInfo.Name : attr.Name;
}

方法的调用可以是:

lblNome.Text = GetDisplay(typeof(Person), "FirstName") + ":";

我可以使用泛型使用更优雅的语法,例如:

string GetDisplay<T>(string property)
{
    PropertyInfo propInfo = typeof(T).GetProperty(property);
    DisplayAttribute attr = propInfo.GetCustomAttributes(false).OfType<DisplayAttribute>().FirstOrDefault();

    return (attr == null) ? propInfo.Name : attr.Name;
}

调用:

GetDisplay<Person>("FirstName");

因此,我想使用 lambda 表达式将调用变得更加优雅,如下所示

GetDisplay<Person>(p => p.FirstName);

:问题是我怎样才能实现这一目标?

I'm building a method to get the DisplayAttribute from System.ComponentModel.DataAnnotations to show on a label for the property.

[Display(Name="First Name")]
public string FirstName { get; set; }

The method is working well:

string GetDisplay(Type dataType, string property)
{
    PropertyInfo propInfo = dataType.GetProperty(property);
    DisplayAttribute attr = propInfo.GetCustomAttributes(false).OfType<DisplayAttribute>().FirstOrDefault();

    return (attr == null) ? propInfo.Name : attr.Name;
}

The call of method can be:

lblNome.Text = GetDisplay(typeof(Person), "FirstName") + ":";

I can use a more elegant sintax using Generics, like:

string GetDisplay<T>(string property)
{
    PropertyInfo propInfo = typeof(T).GetProperty(property);
    DisplayAttribute attr = propInfo.GetCustomAttributes(false).OfType<DisplayAttribute>().FirstOrDefault();

    return (attr == null) ? propInfo.Name : attr.Name;
}

And the call:

GetDisplay<Person>("FirstName");

So, I would like to make it more more elegant using lambda expressions turning the call like this:

GetDisplay<Person>(p => p.FirstName);

The question is how can I achieve this?

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

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

发布评论

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

评论(1

病毒体 2024-12-17 05:19:30

这是我用来获取 lambda 表达式给出的属性名称的方法:

    public static string PropertyName<T1, T2>(this Expression<Func<T1, T2>> expression)
    {
        MemberExpression member = null;
        if (expression.Body is MemberExpression)
            member = expression.Body as MemberExpression;
        if (expression.Body is UnaryExpression && expression.Body.NodeType == ExpressionType.Convert)
            member = ((UnaryExpression)expression.Body).Operand as MemberExpression;
        if (member == null)
            throw new ArgumentException("Selector must point to a property or field.", "expression");
        return member.Member.Name;
    }

使用此方法,您只需调用前面的方法即可:

string GetDisplay<T>(Expression<Func<T, object>> expression)
{
    return GetDisplay<T>(expression.PropertyName());
}

Here is the method I use to fetch property name given by lambda expression:

    public static string PropertyName<T1, T2>(this Expression<Func<T1, T2>> expression)
    {
        MemberExpression member = null;
        if (expression.Body is MemberExpression)
            member = expression.Body as MemberExpression;
        if (expression.Body is UnaryExpression && expression.Body.NodeType == ExpressionType.Convert)
            member = ((UnaryExpression)expression.Body).Operand as MemberExpression;
        if (member == null)
            throw new ArgumentException("Selector must point to a property or field.", "expression");
        return member.Member.Name;
    }

Using this method you can just call the previous method:

string GetDisplay<T>(Expression<Func<T, object>> expression)
{
    return GetDisplay<T>(expression.PropertyName());
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文