问题描述
我正在尝试将System.Linq.Dynamic.Core与ImmutableArray一起用于动态OrderBy,但出现以下异常:
Expression of type 'System.Collections.Immutable.ImmutableArray`1[$customType]' cannot be used for parameter of type 'System.Collections.Generic.IEnumerable`1[$customType]' of method 'System.Linq.IOrderedEnumerable`1[$customType] OrderByDescending[$customType,Int32](System.Collections.Generic.IEnumerable`1[$customType],System.Func`2[$customType,system.int32])' (Parameter 'arg0')
这是为什么?他们是否为不可变类型定义了特定的验证?普通OrderBy完全有能力对其进行排序。应该将其作为功能请求提交给他们,还是我遗漏了一些明显的东西?将其投射到列表后,它就像一个超级按钮。
解决方法
这似乎是.Net(核心)中的错误,尤其是在Expression.Call
的参数验证中。
最终,第一个参数通过调用TypeUtils.AreReferenceAssignable
进行验证,并且代码假定值类型不能(引用)可分配给非值类型,并且ImmutableArray
是值类型,因为它是用struct
实现。
我在github上打开an issue来看看别人的想法。