- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
假设我有一个整数列表 ( 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?
如果输入是通用的,则不是。编译器不能部分推断泛型参数——您要么必须指定所有泛型参数,要么不指定任何泛型参数,让编译器推断。
即使您可以,编译器仍然不允许泛型类型之间的隐式转换。
如果您希望编译器在编译时识别无效的转换,您可以进行显式转换:
var mappedIdentifiersString = identifiers.Select(x => (string)x); // fails at compile time if x is an int.
它可能无法在编译时捕获所有 可能的无效转换(例如,在编译时始终允许向/从 object
进行转换),但它确实会因您的特定 int
而失败。至 string
场景。
关于c# - 使用 Linq 的隐式转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34022707/
我是一名优秀的程序员,十分优秀!