C# CS1503 Select 无法推断类型,由 System.Func 和 Func 混淆

问题描述

我在集合上使用 Select() 来创建许多转换函数。 C# 抱怨 Select 调用无法推断类型,显然它期待 System.Func 但正在获取 Func...?我看不出有什么区别。确切的错误文本:

Argument 1: cannot convert from 'System.Collections.Generic.IEnumerable<Func<Foo,Foo>>' to 'System.Collections.Generic.IEnumerable<System.Func<Foo,Foo>>' [language-core]csharp(CS1503)

稍加解释的代码看起来像(ExtendBy 返回一个 Foo):

BarCollection.Select(b => ((Foo f) => f.ExtendedBy(b)))

如果我明确指定与 Select 相关的类型,它就可以工作,但这很麻烦:

BarCollection.Select<Bar,System.Func<Foo,Foo>>(b => ((Foo f) => f.ExtendedBy(b)))

如果我指定类型但使用 Func 而不是 System.Func,我会得到原始错误,因为未指定类型:

BarCollection.Select<Bar,Func<Foo,Foo>>(b => ((Foo f) => f.ExtendedBy(b)))

所以似乎除了 System.Func 之外的其他一些 Func 正在被使用和混淆。有没有办法解决这个问题,以便 Select 可以隐式确定类型?两段代码在同一个库项目中,TargetFramework设置为net5.0。

作为参考,这里是我调用 Select() 的文件的 using 语句:

using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)