使用 Linq 的隐式转换

Implicit cast using Linq

假设我有一个整数列表 (var identifiers = Enumerable.Empty<int>())。

有了这个列表,我可以将单个项目转换为另一种类型:

var castedIdentifiersLong = identifiers.Cast<long>();
var castedIdentifiersString = identifiers.Cast<string>();

我们可以使用Select<TSource, TTarget>()来使用隐式转换:

var mappedIdentifiersLong = identifiers.Select<int, long>(x => x);
var mappedIdentifiersString = identifiers.Select<int, string>(x => x);

显然最后一条语句失败了,因为 int 不能隐式转换为 string。这是故意的。

有没有一种方法可以定义一个扩展方法(比如 CastImplicitly<T>),我只能在其中定义两个泛型类型之一,它会从可枚举的源中找出第一个类型?

var unwanted = identifiers.ImplicitCast<string>();
var wanted = identifiers.ImplicitCast<long>();

在这种情况下 unwanted 甚至不应该编译,因为 int 不能隐式转换为 string。但另一方面,wanted 应该编译,因为它可以隐式转换为 long.

Is there a way that I can define an extension method (say CastImplicitly<T>), where I can only define one of the two generic types and it would figure out the first type from the source enumerable?

如果输入是通用的,则不会。编译器无法部分推断通用参数 - 您必须指定所有通用参数或 none 并让编译器推断。

即使您可以,编译器仍然不允许泛型类型之间的隐式转换。

如果您希望编译器在编译时识别无效的转换,您可以进行显式转换:

var mappedIdentifiersString = identifiers.Select(x => (string)x);  // fails at compile time if x is an int.

它可能无法在编译时捕获 每个 可能的无效转换(例如,在编译时始终允许转换 to/from object)但是它对于您的特定 intstring 场景确实失败了。