gpt4 book ai didi

c# - 如何使用编译后的 lambda 函数对运行时生成的类型进行分组

转载 作者:太空宇宙 更新时间:2023-11-03 22:41:03 26 4
gpt4 key购买 nike

我有一个针对接口(interface)运行的 compiledLambda 函数。不幸的是,该接口(interface)只是一个标记接口(interface),实际类型是在运行时动态生成的,并且具有我想对其进行分组的属性。

下面是一些示例代码:

class Program
{
static void Main(string[] args)
{
// Just an example assignment: In the real life scenario the dynamic generated class is created during runtime.
IEnumerable<IDynamicGeneratedModelClass> list = GetDataFromService();

// get the 'real' type from the list
LambdaExpression lambdaExpression = DynamicExpression.ParseLambda(list.First().GetType(), typeof(object), "SomeProperty");
Func<IDynamicGeneratedModelClass, object> compiledLambdaFunction = (Func<IDynamicGeneratedModelClass, object>)lambdaExpression.Compile();

// Expected result: Group list on "SomeProp"
var result = list.GroupBy(compiledLambdaFunction);
}

private static IList<IDynamicGeneratedModelClass> GetDataFromService()
{
return new List<IDynamicGeneratedModelClass> {
new DynamicGeneratedModelClass("Class1"),
new DynamicGeneratedModelClass("Class2")
};
}
}

public interface IDynamicGeneratedModelClass
{}

public class DynamicGeneratedModelClass : IDynamicGeneratedModelClass
{
public DynamicGeneratedModelClass(string someProperty)
{
SomeProperty = someProperty;
}

public string SomeProperty { get; }
}

当 lambda 表达式被编译时它抛出以下异常:

System.InvalidCastException: 'Unable to cast object of type 'System.Func`2[ConsoleApp12.DynamicGeneratedModelClass,System.Object]' to type 'System.Func`2[ConsoleApp12.IDynamicGeneratedModelClass,System.Object]'.'

你能告诉我我做错了什么以及如何解决吗?

最佳答案

Func<T, TResult> 的第一个通用参数委托(delegate)声明为 contravariant ( in ),这意味着您可以将派生参数较少的委托(delegate)分配给派生参数较多的委托(delegate),但反之则不行(换句话说,您可以将 Func<IDynamicGeneratedModelClass,Object> 转换为 Func<DynamicGeneratedModelClass,Object>,但不能将 Func<DynamicGeneratedModelClass,Object> 转换为到 Func<IDynamicGeneratedModelClass,Object> )。

为避免此问题,您现在生成的不是 lambda 表达式:

// lambda has "wrong" type Func<DynamicGeneratedModelClass, object>
(DynamicGeneratedModelClass item) => item.SomeProperty

生成与此等效的 lambda:

// lambda now has "correct" type Func<IDynamicGeneratedModelClass, object>
(IDynamicGeneratedModelClass item) => ((DynamicGeneratedModelClass)item).SomeProperty

我不熟悉 DynamicExpression你用来生成 lambda 的库,但这可以很容易地使用 System.Linq.Expression 完成类:

var itemType = list.First().GetType();
var propertyName = "SomeProperty";
var parameterExpr = Expression.Parameter(typeof(IDynamicGeneratedModelClass));
var castExpr = Expression.Convert(parameterExpr, itemType);
var propExpr = Expression.Property(castExpr, propertyName);
var lambdaExpr = Expression.Lambda(propExpr, parameterExpr);

// Compiled lambda is now of type Func<IDynamicGeneratedModelClass, object>
Func<IDynamicGeneratedModelClass, object> compiledLambdaFunction = (Func<IDynamicGeneratedModelClass, object>)lambdaExpr.Compile();

var result = list.GroupBy(compiledLambdaFunction);

关于c# - 如何使用编译后的 lambda 函数对运行时生成的类型进行分组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52168936/

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