F# 将 TextBlock 向上转换为 UIElement
问题出在哪里:
let (x:UIElement) = upcast new TextBlock()
错误是:此处需要类型“System.ComponentModel.ISupportInitialize”,但不可用。您必须添加对程序集“System,Version=4.0.0....”的引用
TextBlock
是 UIElement 的子类型...
请注意,按照错误消息所述执行操作确实可以解决问题,但为什么需要执行向上转换这样的基本操作呢?
What is wrong with:
let (x:UIElement) = upcast new TextBlock()
The error is: The type 'System.ComponentModel.ISupportInitialize' is required here and is unavailable. You must add a reference to assembly 'System, Version=4.0.0....'
TextBlock
is a subtype of UIElement
...
Note that doing what the error message says does solve the issue, but why is that necessary to do something as basic as upcasting?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
正如lasseespeholt在他的(现已删除?)答案中提到的,您的代码没有任何问题,您只需按照错误消息的提示添加对
System.dll
的引用。但是发生了什么事?
您在该特定行上收到错误消息,因为它是编译器首先遇到 System.dll 库中某种类型的地方(接口
ISupportInitialize
,它是由TextBlock
实现)并意识到它需要对库的引用才能理解类型。获得相同错误消息的另一种方法是这样写:
在这种情况下,IntelliSense 需要查看类型(以便它可以填充成员完成)。
As lasseespeholt mentioned in his (now deleted?) answer, there is nothing wrong with your code and you just need to add the reference to
System.dll
as the error message suggests.But what is going on?
You are getting the error message on that particular line, because it is the first place where the compiler encounters some type from the
System.dll
library (an interfaceISupportInitialize
, which is implemented byTextBlock
) and realizes that it needs the reference to the library in order to understand the type.Another way to get the same error message is to write this:
In this case, the IntelliSense needs to look at the type (so that it can populate member completion).
来自文档:
“在许多面向对象的语言中,向上转换是隐式;在 F# 中,当您将参数传递给对象类型的方法时,会自动应用向上转换,但是,对于模块中的 let 绑定函数,向上转换不是自动的,除非将参数类型声明为 a。有关详细信息,请参阅灵活类型 (F#)。 ”
如果您使用以下语法:
您的代码将使用灵活类型(由
#
指示)并且可以编译。但是,现在您会收到警告:“此构造导致代码不如其类型注释所指示的通用。在 'c' 处或附近使用 '#'、'_' 或其他类型注释所隐含的类型变量:\path\Program.fs' 已被限制为“TextBlock”类型。”
From the documentation:
"In many object-oriented languages, upcasting is implicit; in F#, the rules are slightly different. Upcasting is applied automatically when you pass arguments to methods on an object type. However, for let-bound functions in a module, upcasting is not automatic, unless the parameter type is declared as a flexible type. For more information, see Flexible Types (F#)."
If you'd use the following syntax:
your code would use a flexible type (indicated by the
#
) and it would compile. However, now you would get a warning:"This construct causes code to be less generic than indicated by its type annotations. The type variable implied by the use of a '#', '_' or other type annotation at or near 'c:\path\Program.fs' has been constrained to be type 'TextBlock'."