简体   繁体   English

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

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

I am trying to create an expression of the following form: 我正在尝试创建以下形式的表达式:

e => e.CreationDate;

CreationDate is of type long , however I want the expression to return an object instead. CreationDate的类型为long ,但我希望表达式返回一个object

I want to use object as a return type because the expression is built dynamically at runtime based on a query paramater. 我想使用object作为返回类型,因为表达式是在运行时基于查询参数动态构建的。 The query parameter specifies the property to access in the expression, such as: query参数指定要在表达式中访问的属性,例如:

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

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. 如您所见,我可以使用不同类型的不同属性进行排序,因此返回类型object将允许我将表达式构建为尽可能通用。

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

I get the following exception: 我得到以下异常:

Expression of type 'System.Int64' cannot be used for return type 'System.Object' “System.Int64”类型的表达式不能用于返回类型“System.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). 这很奇怪,因为据我所知,所有类型都从object扩展(似乎表达式树还没有支持多态)。

Nevertheless, I searched on the web and stumbled with this similar question: 然而,我在网上搜索并偶然发现了类似的问题:

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

Following Jon Skeet 's answer, I modified my last line to: 按照Jon Skeet的回答,我修改了我的最后一行:

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)

I cannot use this solution, because later in the program an exception is thrown if the expression body is not a MemberExpression (ie, a member access operation) 我不能使用这个解决方案,因为如果表达式主体不是MemberExpression (即成员访问操作),程序后面会抛出异常

I kept searching on the Internet for a satisfying answer, but couldn't find any. 我一直在互联网上搜索一个令人满意的答案,但找不到任何答案。

How can I achieve e => e.CreationDate where return type is object ? 如何在返回类型为object e => e.CreationDate下实现e => e.CreationDate

Depending on how you use result you could create it dynamically with the delegate type Func<Entity, long> and type it as a LambdaExpression : 根据您使用result您可以使用委托类型Func<Entity, long>动态创建它Func<Entity, long>并将其键入为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);

Short answer: no, it is not possible. 简答:不,这是不可能的。 Value types need to be boxed to be seen as objects. 需要将值类型加框以将其视为对象。 The compiler does it for you normally, but if you build code yourself (eg expression trees), you need to specify it as an explicit conversion, the way you see it in the found answer. 编译器通常会为您执行此操作,但如果您自己构建代码(例如表达式树),则需要将其指定为显式转换,就像您在找到的答案中看到的那样。 If you cannot have it as a non-generic LambdaExpression, I would handle the convert case additionally where you expect the MemberExpression, or use PropertyInfo, and construct the orderby Expression only in the last moment. 如果您不能将它作为非泛型的LambdaExpression,我会在您期望MemberExpression或使用PropertyInfo的情况下另外处理转换情况,并仅在最后一刻构造orderby Expression。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 类型'System.Int32'的表达式不能用于返回类型'System.Object' - Expression of type 'System.Int32' cannot be used for return type 'System.Object' &#39;System.Int16&#39;类型的表达式不能用于返回类型&#39;System.Object&#39; - Expression of type 'System.Int16' cannot be used for return type 'System.Object' Expression.Convert:'System.Int64'类型的对象无法转换为'System.Int32'类型 - Expression.Convert: Object of type 'System.Int64' cannot be converted to type 'System.Int32' 类型'System.Int32'的表达式不能用于方法'Boolean Equals(System.Object)'的'System.Object'类型的参数 - Expression of type 'System.Int32' cannot be used for parameter of type 'System.Object' of method 'Boolean Equals(System.Object)' 类型&#39;System.Int32&#39;的表达式不能用于方法&#39;Boolean Equals(System.Object)&#39;的类型&#39;System.Object&#39;的参数 - Expression of type 'System.Int32' cannot be used for parameter of type 'System.Object' of method 'Boolean Equals(System.Object)' Linq和Equality Operator:类型'System.Int32'的表达式不能用于'System.Object'类型的参数 - Linq and the Equality Operator: Expression of type 'System.Int32' cannot be used for parameter of type 'System.Object' &#39;System.DateTime&#39;类型的表达式不能用于返回类型&#39;System.Object&#39; - Expression of type 'System.DateTime' cannot be used for return type 'System.Object' Autofac.Core.DependencyResolutionException:类型“”的表达式不能用于 Autofac 6.2 中的返回类型“System.Object” - Autofac.Core.DependencyResolutionException: Expression of type '' cannot be used for return type 'System.Object' in Autofac 6.2 类型“ Territory_Manager.HtmlAddressGenerator + Entry”的表达式不能用于返回类型“ System.Object” - Expression of type 'Territory_Manager.HtmlAddressGenerator+Entry' cannot be used for return type 'System.Object' 用于转换数值的表达式:System.InvalidCastException:&gt; 无法将“System.Int32”类型的 object 转换为“System.Int64”类型 - Expression to convert numeric values: System.InvalidCastException: > Unable to cast object of type 'System.Int32' to type 'System.Int64'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM