F#upcasting TextBlock到UIElement

Jul*_*les 3 .net f# uielement

出什么问题了:

let (x:UIElement) = upcast new TextBlock()
Run Code Online (Sandbox Code Playgroud)

错误是:此处需要类型为"System.ComponentModel.ISupportInitialize"且不可用.您必须添加对程序集'System,Version = 4.0.0 ....'的引用.

TextBlock UIElement ... 的子类型

请注意,执行错误消息所说明的内容确实可以解决问题,但为什么有必要做一些基本的转发?

Tom*_*cek 8

正如lasseespeholt在他的(现已删除?)答案中提到的那样,您的代码没有任何问题,您只需要添加引用,System.dll如错误消息所示.

但是发生了什么? 您正在获取该特定行上的错误消息,因为它是编译器从System.dll库(接口ISupportInitialize,由其实现TextBlock)遇到某种类型的第一个位置,并且意识到它需要对库的引用才能理解类型.

获取相同错误消息的另一种方法是写这个:

let x = new TextBlock() 
x.  // If you get IntelliSense here, you'll see just '<Note>' 
    // with the same error message as the one you're getting
Run Code Online (Sandbox Code Playgroud)

在这种情况下,IntelliSense需要查看类型(以便它可以填充成员完成).