如何获取泛型表达式中使用的属性的类型?

发布于 2024-11-17 14:20:46 字数 377 浏览 2 评论 0原文

public static void Map<T>(Expression<Func<T, object >> expression)
{
    var memberExpression = (expression.Body as UnaryExpression).Operand as MemberExpression;
    var name = memberExpression.Member.Name;
}

用法:

Map<Article>(x => x.Name)

名称是字符串类型。

我如何获得类型?

public static void Map<T>(Expression<Func<T, object >> expression)
{
    var memberExpression = (expression.Body as UnaryExpression).Operand as MemberExpression;
    var name = memberExpression.Member.Name;
}

Usage:

Map<Article>(x => x.Name)

Name is of type string.

How do I get the type?

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

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

发布评论

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

评论(4

骑趴 2024-11-24 14:20:46
var returnType = memberExpression.Member is MethodInfo 
    ? ((MethodInfo)memberExpression.Member).ReturnType 
    : ((PropertyInfo)memberExpression.Member).PropertyType;

我假设你想要返回类型。如果您想要定义方法的类型,请使用memberExpression.Member.DeclaringType。

var returnType = memberExpression.Member is MethodInfo 
    ? ((MethodInfo)memberExpression.Member).ReturnType 
    : ((PropertyInfo)memberExpression.Member).PropertyType;

I assume you want the return type. If you want the type on which the method is defined, use memberExpression.Member.DeclaringType.

孤单情人 2024-11-24 14:20:46

如果您知道它始终是一个属性(而不是字段),您可以这样做:

public static void Map<T>(Expression<Func<T, object>> expression)
{
    var memberExpression = (MemberExpression)expression.Body;
    var property = (PropertyInfo)memberExpression.Member;
    var name = property.Name.Dump();
    var type = property.PropertyType.Dump();
}

这也修复了我在评论中提到的错误。

If you know that it will always be a property (and not a field), you can do it like this:

public static void Map<T>(Expression<Func<T, object>> expression)
{
    var memberExpression = (MemberExpression)expression.Body;
    var property = (PropertyInfo)memberExpression.Member;
    var name = property.Name.Dump();
    var type = property.PropertyType.Dump();
}

This also fixes the error I mentioned in a comment.

瞎闹 2024-11-24 14:20:46

这是一个简单的表达式扩展方法,可能对其他人有用。

 public static Type GetPropertyType<TModel, TValue>(this Expression<Func<TModel, TValue>> expression)
    {
        var body = expression.Body as MemberExpression;
        if (body == null)
        {
            throw new ArgumentException("'expression' should be a member expression");
        }
        return ((PropertyInfo)body.Member).PropertyType;
    }

使用示例:

if (expression.GetPropertyType() == typeof(DateTime))

Here is a simple Expression Extension Method that may be useful to others.

 public static Type GetPropertyType<TModel, TValue>(this Expression<Func<TModel, TValue>> expression)
    {
        var body = expression.Body as MemberExpression;
        if (body == null)
        {
            throw new ArgumentException("'expression' should be a member expression");
        }
        return ((PropertyInfo)body.Member).PropertyType;
    }

Sample usage:

if (expression.GetPropertyType() == typeof(DateTime))
初见 2024-11-24 14:20:46

应该不会太难:

x.Name.GetType()

Shouldn't be too difficult:

x.Name.GetType()

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