演员 vs “as”重新审视运营商
我知道已经有几篇文章涉及强制转换和 as
运算符之间的区别。它们大多都重申了相同的事实:
as
运算符不会抛出异常,但如果转换失败则返回null
- 因此,
as
运算符只能起作用对于引用类型 as
运算符不会使用用户定义的转换运算符
然后答案往往会无休止地争论如何使用或不使用其中一个以及每个的优缺点,甚至它们的性能(我一点也不感兴趣)。
但这里还有更多的事情在起作用。想一想:
static void MyGenericMethod<T>(T foo)
{
var myBar1 = foo as Bar; // compiles
var myBar2 = (Bar)foo; // does not compile ('Cannot cast expression of
// type 'T' to type 'Bar')
}
请不要介意这个明显令人悔恨的例子是否是好的做法。我在这里担心的是两者之间非常有趣的差异,因为强制转换不会编译,而 as
可以编译。我真的很想知道是否有人可以对此有所了解。
正如人们经常指出的那样,as
运算符忽略用户定义的转换,但在上面的示例中,它显然是两者中更强大的一个。请注意,就编译器而言,(编译时未知)类型 T 和 Bar 之间没有已知的联系。演员阵容完全是“运行时”的。我们是否应该怀疑强制转换在编译时已全部或部分解析,而 as
运算符则没有?
顺便说一句,添加类型约束毫不奇怪地修复了强制转换,因此:
static void MyGenericMethod<T>(T foo) where T : Bar
{
var myBar1 = foo as Bar; // compiles
var myBar2 = (Bar)foo; // now also compiles
}
为什么 as
运算符可以编译,而强制转换却不能编译?
I know there are several posts already concerning the difference between casts and the as
operator. They all mostly restate the same facts:
- The
as
operator will not throw, but returnnull
if the cast fails - Consequently, the
as
operator only works with reference types - The
as
operator will not use user-defined conversion operators
Answers then tend to debate endlessly the how to use or not use the one or the other and the pros and cons of each, even their performance (which interests me not at all).
But there is something more at work here. Consider:
static void MyGenericMethod<T>(T foo)
{
var myBar1 = foo as Bar; // compiles
var myBar2 = (Bar)foo; // does not compile ('Cannot cast expression of
// type 'T' to type 'Bar')
}
Please never mind whether this obviously contrite example is good practice or not. My concern here is the very interesting disparity between the two in that the cast will not compile whereas the as
does. I really would like to know if anyone could shed some light on this.
As is often noted, the as
operator disregards user-defined conversions, but in the above example, it is clearly the more capable of the two. Note that as
far as the compiler is concerned, there is no known connection between the (unknown at compile-time) type T and Bar. The cast is entirely 'run-time'. Should we suspect that the cast is resolved, wholly or partly, at compile time and the as
operator not?
By the way, adding a type constraint unsurprisingly fixes the cast, thus:
static void MyGenericMethod<T>(T foo) where T : Bar
{
var myBar1 = foo as Bar; // compiles
var myBar2 = (Bar)foo; // now also compiles
}
Why does the as
operator compile and the cast not?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
要解决您的第一个问题:不仅仅是
as
运算符忽略用户定义的转换,尽管这是相关的。更相关的是强制转换运算符做了两件相互矛盾的事情。强制转换运算符意味着:我知道这个编译时类型 Foo 的表达式实际上是运行时类型 Bar 的对象。编译器,我现在告诉你这个事实,以便你可以利用它。请假设我是正确的生成代码;如果我不正确,那么你可能会在运行时抛出异常。
我知道这个编译时类型 Foo 的表达式实际上是运行时类型 Foo。有一种标准方法可以将 Foo 的部分或全部实例转换为 Bar 的实例。编译器请生成这样一个转换,如果在运行时发现被转换的值不可转换,则在运行时抛出异常。
这些是相反。巧妙的技巧,让一个操作符做相反的事情。
相比之下,as 运算符仅具有第一种含义。
as
仅执行装箱、拆箱 和表示保留 转换。强制转换可以完成所有这些操作以及额外的表示更改转换。例如,将 int 转换为 Short 会将表示形式从四字节整数更改为两字节整数。这就是为什么“原始”强制转换对于不受约束的泛型是不合法的。因为编译器没有足够的信息来确定它是什么类型的转换:装箱、拆箱、保留表示或更改表示。用户的期望是泛型代码中的强制转换具有更强类型代码中强制转换的所有语义,而我们无法有效地生成该代码。
考虑一下:
你希望这有效吗?我们生成哪些代码可以处理:
基本上,我们必须为测试发出代码,再次启动编译器,对表达式进行全面分析,然后发出新代码。我们在 C# 4 中实现了该功能;它被称为“动态”,如果这是您想要的行为,您可以随意使用它。
as
不存在这些问题,因为as
只做了三件事。它执行装箱转换、拆箱转换和类型测试,我们可以轻松生成执行这三件事的代码。To address your first question: it is not just that the
as
operator disregards user-defined conversions, though that is relevant. What is more relevant is that the cast operator does two contradictory things. The cast operator means either:I know that this expression of compile-time type Foo will actually be an object of runtime-type Bar. Compiler, I am telling you this fact now so that you can make use of it. Please generate code assuming that I am correct; if I am incorrect, then you may throw an exception at runtime.
I know that this expression of compile-time type Foo will actually be of runtime type Foo. There is a standard way of converting some or all instances of Foo to an instance of Bar. Compiler, please generate such a conversion, and if it turns out at runtime that the value being converted is not convertible, then throw an exception at runtime.
Those are opposites. Neat trick, to have an operator that does opposite things.
The
as
operator by contrast only has the first sense. Anas
only does boxing, unboxing and representation-preserving conversions. A cast can do all of those plus additional representation-changing conversions. For example, casting int to short changes the representation from a four-byte integer to a two-byte integer.That's why "raw" casts are not legal on unconstrained generics; because the compiler does not have enough information to figure out what kind of cast it is: boxing, unboxing, representation-preserving or representation-changing. The expectation of users is that a cast in generic code has all the semantics of a cast in more strongly typed code, and we have no way to generate that code efficiently.
Consider:
Do you expect that to work? What code do we generate that can handle:
Basically we would have to emit code for the test that started the compiler again, did a full analysis of the expressions, and then emitted new code. We implemented that feature in C# 4; it's called "dynamic" and if that's the behaviour you want, you can feel free to use it.
We have none of these problems with
as
, becauseas
only does three things. It does boxing conversions, unboxing conversions, and type tests, and we can easily generate code that does those three things.您在问题开始时自己给出了答案:“as 运算符不会使用用户定义的转换运算符” - 同时,强制转换会,这意味着它需要找到这些运算符( >或它们不存在)在编译时。
T 类型未知这一事实意味着编译器无法知道它和Bar之间是否没有联系。
请注意,
(Bar)(object)foo
确实有效,因为没有类型可以有到 Object 的转换运算符[因为它是所有东西的基类],并且从 object 到 Bar 的转换已知不必处理转换运算符。You gave the answer yourself at the beginning of your question: "The as operator will not use user-defined conversion operators" - meanwhile, the cast does, which means it needs to find those operators (or their absence) at compile time.
The fact that the T type is unknown means the compiler can't know whether or not there is no connection between it and Bar.
Note that
(Bar)(object)foo
does work, because no type can have a conversion operator to Object [since it is the base class of everything], and the cast from object to Bar is known to not have to deal with a conversion operator.这是类型安全的问题。
任何
T
都不能转换为Bar
,但任何T
都可以“视为”作为
Bar
,因为即使没有从T
到Bar
的转换,行为也已明确定义。It is a matter of type safety.
Any
T
cannot by convert to aBar
, but anyT
can be "seen"as
aBar
since the behavior is well defined even if there is no conversion fromT
toBar
.第一个编译器之所以能够编译,是因为
as
关键字就是这样定义的。如果无法转换,它将返回null
。它是安全的,因为as
关键字本身不会导致任何运行时问题。您可能已检查或未检查变量是否为空这一事实是另一回事。将
as
视为 TryCast 方法。The first compiles simply because that's how the
as
keyword is defined. If it can't be cast, it will returnnull
. Its safe because theas
keyword by itself won't cause any runtime issues. The fact that you may or may not have checked for the varible to be null is another matter.Think of
as
as a TryCast method.编译器不知道如何生成适用于所有情况的代码。
考虑这两个调用:
现在假设
Foo1
包含一个强制转换运算符,可以将其转换为Bar
实例,并且Foo2
源自>Bar
代替。显然,所涉及的代码在很大程度上取决于您传入的实际T
。在您的特定情况下,您说该类型已经是
Bar
类型,因此显然编译器可以这样做引用转换,因为它知道这是安全的,没有正在进行或不需要的转换。现在,as 转换更具“探索性”,它不仅不考虑用户转换,而且明确允许强制转换毫无意义的事实,因此编译器对此不予理睬。
The compiler does not know how to generate code that will work for all cases.
Consider these two calls:
now assume that
Foo1
contains a cast operator that can convert it to aBar
instance, and thatFoo2
descends fromBar
instead. Obviously the code involved would depend heavily on the actualT
you pass in.In your particular case you say that the type is already a
Bar
type so obviously the compiler can just do a reference conversion, because it knows that is safe, there's no conversion going on or needed.Now, the
as
conversion is more "exploratory", not only does it not consider user conversions, it explicitly allows for the fact that the cast is meaningless, so the compiler let that slide.