gpt4 book ai didi

c# - 无法使用 Int64 转换类型为 System.Func`2 的对象

转载 作者:太空狗 更新时间:2023-10-29 23:47:18 24 4
gpt4 key购买 nike

using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;
using System.Web.UI.WebControls;
using dynamic = System.Linq.Dynamic;
using System.Linq.Expressions;

namespace Project.Lib.Extensions
{
public static partial class Utils
{
public static List<T> SortForMe<T>(this List<T> list, string propertyName,SortDirection sortDirection)
{
string exp1 = string.Format("model.{0}", propertyName);
var p1 = Expression.Parameter(typeof(T), "model");
var e1 = dynamic.DynamicExpression.ParseLambda(new[] { p1 }, null, exp1);

if (e1 != null)
{
if (sortDirection==SortDirection.Ascending)
{
var result = list.OrderBy((Func<T, object>)e1.Compile()).ToList();
return result;
}
else
{
var result = list.OrderByDescending((Func<T, object>)e1.Compile()).ToList();
return result;
}
}
return list;
}
}
}

我正在使用此代码按 propertyName 对我的通用列表进行排序。当属性类型为 string 时,此代码运行成功,但是当类型为 long 时或 int ,我得到这个异常(exception):

Unable to cast object of type 'System.Func`2[Project.Lib.Model.UserQueryCount,System.Int64]' to type 'System.Func`2[Project.Lib.Model.UserQueryCount,System.Object]'.


var result = list.OrderBy((Func<T, dyamic>)e1.Compile()).ToList();

在上面的行中,我决定使用 dynamic ,但又得到了异常。我该怎么办?


我改变了我的方法是这样的:

public static List<TModel> SortForMe<TModel>(this List<TModel> list, string propertyName,SortDirection sortDirection) where TModel:class
{
var ins = Activator.CreateInstance<TModel>();
var prop= ins.GetType().GetProperty(propertyName);
var propertyType = prop.PropertyType;

string exp1 = string.Format("model.{0}", propertyName);
var p1 = System.Linq.Expressions.Expression.Parameter(typeof(TModel), "model");
var e1 = System.Linq.Dynamic.DynamicExpression.ParseLambda(new[] { p1 }, null, exp1);

if (e1 != null)
{
if (sortDirection==SortDirection.Ascending)
{
return list.OrderBy((Func<TModel, propertyType>)e1.Compile()).ToList();
}

return list.OrderByDescending((Func<TModel, propertyType>)e1.Compile()).ToList();
}
return list;
}

我使用反射获得了 propertyType 但在 Func 中我不能像这样使用它:"Func<TModel, propertyType>"有什么办法可以解决这个问题

感谢您的帮助。

最佳答案

它不起作用的原因确实是拳击,正如另一个答案中提到的那样。

class S
{
public int f;
public S s;
}

{
Func<S, S> sGetter = s => s.s; // okay
Func<S, object> objectGetter = s => s.s; // okay
objectGetter = sGetter; // also okay
}

{
Func<S, int> intGetter = s => s.f; // okay
Func<S, object> objectGetter = s => s.f; // still okay
objectGetter = intGetter; // not okay
}

Func<S, object> objectGetter = s => s.f的原因有效,但最后一个作业无效是它实际上被解释为 Func<S, object> objectGetter = s => (object)s.f ,并且该 Actor 执行真正的操作。 intGetterobjectGetter不要做同样的事情,所以一个不能被重新解释为另一个。意识到这一点,如果你自己包括那个角色,你应该没问题。据我所知,如果您简单地指定所需的返回类型为 object,DQL(这就是您正在使用的,对吗?)将为您完成此操作。 :

var e1 = DynamicExpression.ParseLambda(new[] { p1 }, typeof(object), exp1);

关于c# - 无法使用 Int64 转换类型为 System.Func`2 的对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11796125/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com