简体   繁体   中英

How do I invoke an F# function from the VS2010 immediate window

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 - eg as an annotation - so that it gets loaded).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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