简体   繁体   English

TryCast在DirectCast工作的地方失败(.NET 4.0)

[英]TryCast fails where DirectCast works (.NET 4.0)

I find this behavior of TryCast in .NET 4.0 / VS 2010 rather confusing. 我发现.NET 4.0 / VS 2010中TryCast的这种行为相当令人困惑。

In my understanding TryCast works like DirectCast, but will return Nothing instead of throwing an exception if a cast is not possible. 在我的理解中,TryCast的工作方式与DirectCast类似,但如果无法进行强制转换,则会返回Nothing而不是抛出异常。

VS 2010 / .NET 4 VS 2010 / .NET 4

?TryCast(CType(1, Object), String)
Nothing
?DirectCast(CType(1, Object), String)
"1"

VS 2008 / .NET 3.5 VS 2008 / .NET 3.5

?TryCast(CType(1, Object), String)
Nothing
?DirectCast(CType(1, Object), String)
Cannot convert to 'String'.

The .NET 3.5 results are consistent with what I believe TryCast does... .NET 4 however is not. .NET 3.5的结果与我认为TryCast的结果一致......但.NET不是。

Can someone please point me in the best direction to securely cast an object to String in .NET 4? 有人可以指出我在.NET 4中将对象安全地转换为String的最佳方向吗?

Based on your code samples starting with the ? 根据您的代码示例开始? I'm guessing you're using the immediate window to perform your test correct? 我猜你正在使用即时窗口来正确执行测试? The problem with this approach is that the immediate window is an interpretation instead of an actual evaluation. 这种方法的问题在于,即时窗口是解释而不是实际评估。 This leaves it susceptible to subtle corner case bugs and this is indeed one of them. 这使得它容易受到细微的角落错误的影响,这确实是其中之一。

If you take your sample code and add it to a simple VB.Net console application you'll find that the 2010 behavior is identical to the 2008 behavior (throws an exception). 如果您获取示例代码并将其添加到简单的VB.Net控制台应用程序,您将发现2010行为与2008行为相同(抛出异常)。

EDIT 编辑

So why did this regression happen? 那为什么会发生这种倒退呢? In 2010 I completely rewrote the VB EE debugging engine (expression evaluator). 在2010年,我完全重写了VB EE调试引擎(表达式评估程序)。 The older code base I inherited was simply too costly to maintain anymore. 我继承的旧代码库太昂贵了,无法维护。 To the point that adding new features to the engine was more expensive that rewriting it from scratch with a better architecture that included the new features. 到了为引擎添加新功能的代价更高,从头开始用包含新功能的更好架构重写它。

As said before debugging evaluations is an interpretation more than an execution of code. 如前所述,调试评估不仅仅是代码执行的解释。 It forces the duplication of some algorithms between the EE and CLR / Compiler. 它强制在EE和CLR /编译器之间复制一些算法。 One of the areas where duplication occurs is in the casting logic. 发生重复的一个领域是在构建逻辑中。 There is no way to ask the CLR debugger to cast a debug time object, it's the responsibility of the EE to determine if the language specified cast is indeed valid. 无法要求CLR调试器转换调试时对象,EE负责确定指定的语言是否确实有效。

The old EE casting logic had numerous bugs (especially in the area of generics and arrays). 旧的EE构建逻辑有许多错误(特别是在泛型和数组领域)。 The newer infrastructure conforms very closely to the CLR guidelines. 较新的基础设施与CLR指南非常接近。 However you'll never have 100% parity because it would disallow very useful expressions in the EE (I may write a blog post on this in the future). 但是你永远不会有100%的平价,因为它会禁止在EE中使用非常有用的表达式(我可能会在将来写一篇博文)。 But for most cases the behavior holds. 但对于大多数情况来说,这种行

In this particular instance I added a subtle bug which allows for a DirectCast of a value which is typed to Object to use VB's Runtime Conversion operators vs. the specified behavior which only allows for CLR conversions. 在这个特定的实例中,我添加了一个微妙的错误,允许DirectCast类型为Object的值使用VB的运行时转换运算符与仅允许CLR转换的指定行为。 Hence this conversion succeeds where it should fail. 因此,这种转换在失败的地方成功。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM