开发者

Can I use F# Interactive to script or interactively debug/test my C# GUI projects?

开发者 https://www.devze.com 2022-12-09 21:20 出处:网络
I have seen a demo of F# and DirectX. User selects a part of F# code and sends it to F# interactive. It between, the form\'s thread is working: the form shows dynamic content and resp开发者_开发知识库

I have seen a demo of F# and DirectX.

User selects a part of F# code and sends it to F# interactive. It between, the form's thread is working: the form shows dynamic content and resp开发者_开发知识库onds to clicks.

Can I conclude that for existing C# winforms project I can create a F# project which references it and then launch the F# project in F# interactive to run arbitrary methods of a form ?

EDIT: what are the steps to invoke btShowNextNumber_Click in a running application ?

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    int i = 0;

    private void btShowNextNumber_Click(object sender, EventArgs e)
    {
        ++i;
        label1.Text = i.ToString();
    }
}


Sure. In F# interactive, you'd run something like:

#r "MyDllName.dll"
open MyNamespace

// Call the constructor to get a new form, then show it
let form = Form1()
form.Show()

// Call whatever public method you want
form.MyMethodThatIWantToUse()    


You can also add:

Debugger.Launch()

to the script. This will bring up a dialog asking which debugger to use. E.g. you can use the same visual studio instance as the one you would use for executing the script.


Sure, but you must follow CLS compliant rules. More info here: http://www.devarticles.com/c/a/C-Sharp/Making-Your-Code-CLS-Compliant/ http://msdn.microsoft.com/en-us/library/730f1wy3.aspx


F# is just such CLR language as C# or VB are. This means that it may reference CLR assemblies (both DLLs and EXEs), import public classes from them and invoke public methods and properties. If class, method or property aren't public you can still access them using reflection, but there're few cases you really should do it.

Note that for WinForm UI elements to respond on user actions you'll need to run windows message loop (using Application.Run). In case of F# shell, it seems, they are running it in a background thread and post actions to this thread using Control.Invoke or its equivalents (WindowsFormsSynchronizationContext).

To invoke method you've shown in the sample you need do one of the following:

1) if method btShowNextNumber_Click is public:

form.Invoke(new Action(()=> form.btShowNextNumber_Click(form, EventArgs.Empty)));

or

SynchronizationContext.Current.Send(o => form.btShowNextNumber_Click(form, EventArgs.Empty));

2) if method btShowNextNumber_Click is not public, but Button btShowNextNumber is:

form.Invoke(new Action(()=> form.btShowNextNumber.PerformClick());

or

SynchronizationContext.Current.Send(o => form.btShowNextNumber.PerformClick());

As far as I understand the way F# shell works, it can be setted up post commands you post through SynchronizationContext.Send transparently, just like IronPython shell does. Can't say how exactly this works for now. Need to check


You need a debugger that supports managed code. It does not matter which language is used to write the host of your code. I debug my code in different containers like MS Office, IE, Windows Explorer, Visual Studio and other VB/C# apps all the time.

0

精彩评论

暂无评论...
验证码 换一张
取 消