问题描述
在调试F#应用程序时,我希望能够从VS2010立即窗口调用F#方法,但似乎不起作用。问题似乎是F#方法实际上是FSharpFunc对象。我尝试使用Invoke方法,但交互式窗口无法识别它。 Visual Studio的F#集成不在立即窗口(或手表)中不支持F#表达式,所以唯一的选择是编写与F#使用的编译表示相对应的C#代码。我尝试这样做,我有与您所描述的相同的问题 - Invoke
方法似乎在(Reflector)中,但Visual Studio不想调用它直接我尝试使用以下示例:
let foo f =
let n = f 1 2 // Breakpoint here
n + 1
然而,还有其他方法可以调用该函数。在这种情况下,F#编译器生成的实际代码是调用 InvokeFast
方法。如果您在直接窗口中输入以下内容,它将起作用:
Microsoft.FSharp.Core.FSharpFunc< int,int> ;. InvokeFast< int>(f,1,2)|
也可以调用通常的 Invoke
dynamic (证明该方法实际上在那里!):
((动态)f).Invoke(1,2)
仅当您添加对 Microsoft.CSharp.dll
的引用(并使用某些在程序集中定义的类型,例如作为注释,以便它被加载)。
While debugging an F# application, I would like to be able to invoke an F# method from the VS2010 immediate window but it doesn't seem to work. The problem appears to be that F# methods are actually FSharpFunc objects. I tried using the "Invoke" method but the interactive window doesn't recognize it.
The F# integration for Visual Studio doesn't support F# expressions in immediate window (or watches), so the only option is to write C# code corresponding to the compiled representation that F# uses. I tried doing that and I'm having the same issue as you described - the Invoke
method appears to be there (in Reflector), but Visual Studio doesn't want to call it directly. I tried it using the following example:
let foo f =
let n = f 1 2 // Breakpoint here
n + 1
However, there are other ways to call the function. In this case, the actual code generated by the F# compiler is a call to InvokeFast
method. If you type the following to the immediate window, it works:
Microsoft.FSharp.Core.FSharpFunc<int, int>.InvokeFast<int>(f, 1, 2) |
It also appears that you can call the usual Invoke
method using dynamic
from C# 4.0 (proving that the method is actually there!):
((dynamic)f).Invoke(1, 2)
This works only if you add reference to Microsoft.CSharp.dll
(and use some type defined in the assembly somewhere in your code - e.g. as an annotation - so that it gets loaded).
这篇关于如何从VS2010立即窗口调用F#函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!