类型'System.Int64'的表达式不能用于返回类型'System.Object' [英] Expression of type 'System.Int64' cannot be used for return type 'System.Object'

查看:524
本文介绍了类型'System.Int64'的表达式不能用于返回类型'System.Object'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建以下形式的表达式:

I am trying to create an expression of the following form:

e => e.CreationDate;

CreationDate的类型为long,但是我希望表达式返回一个object.

CreationDate is of type long, however I want the expression to return an object instead.

我想使用object作为返回类型,因为该表达式是在运行时基于查询参数动态构建的. query参数指定要在表达式中访问的属性,例如:

I want to use object as a return type because the expression is built dynamically at runtime based on a query paramater. The query parameter specifies the property to access in the expression, such as:

> entities?order=creationDate
> entities?order=score

如您所见,我可以按具有不同类型的不同属性进行排序,因此返回类型object将使我可以构建尽可能通用的表达式.

As you can see, I can order by different properties with different types, so a return type object would allow me to build the expression as generic as possible.

问题是当我尝试创建表达式时:

The problem is that when I try to create the expression:

ParameterExpression entityParameter = Expression.Parameter(typeof(Entity), "e");
Expression propertyAccess = Expression.Property(entityParameter, property);
Expression<Func<Entity, object>> result = Expression.Lambda<Func<Entity, object>>(propertyAccess, entityParameter);

我收到以下异常:

类型'System.Int64'的表达式不能用于返回类型 'System.Object'

Expression of type 'System.Int64' cannot be used for return type 'System.Object'

这很奇怪,因为据我所知,所有类型都是从object扩展的(表达式树似乎还不支持多态).

It is strange, because as far as I know, all types extend from object (It seems polymorphism is not yet supported by expression trees).

尽管如此,我还是在网上搜索并偶然发现了这个类似的问题:

Nevertheless, I searched on the web and stumbled with this similar question:

类型表示'System.Int32'不能用于返回类型'System.Object'

按照 Jon Skeet 的回答,我将最后一行修改为:

Following Jon Skeet's answer, I modified my last line to:

Expression<Func<Entity, object>> result = Expression.Lambda<Func<Entity, object>>(Expression.Convert(propertyAccess, typeof(object)), entityParameter);

这可以正常工作,但是不会生成我想要的表达式.相反,它会生成如下内容:

This works fine, but it doesn't generate the expression I want. Instead, it generates something like this:

e => Convert(e.CreationDate)

我无法使用此解决方案,因为如果表达式主体不是MemberExpression(即成员访问操作),则在程序后面会引发异常

I cannot use this solution, because later in the program an exception is thrown if the expression body is not a MemberExpression (i.e., a member access operation)

我一直在互联网上搜索令人满意的答案,但找不到任何答案.

I kept searching on the Internet for a satisfying answer, but couldn't find any.

在返回类型为object的情况下,如何实现e => e.CreationDate?

How can I achieve e => e.CreationDate where return type is object?

推荐答案

根据您使用result的方式,可以使用委托类型Func<Entity, long>动态创建它并将其键入为LambdaExpression:

Depending on how you use result you could create it dynamically with the delegate type Func<Entity, long> and type it as a LambdaExpression:

ParameterExpression entityParameter = Expression.Parameter(typeof(Entity), "e");
Expression propertyAccess = Expression.Property(entityParameter, property);
var funcType = typeof(Func<,>).MakeGenericType(typeof(Entity), property.PropertyType);
LambdaExpression result = Expression.Lambda(funcType, propertyAccess, entityParameter);

这篇关于类型'System.Int64'的表达式不能用于返回类型'System.Object'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆