简体   繁体   English

我可以使用F#Interactive来编写脚本或交互式调试/测试我的C#GUI项目吗?

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

I have seen a demo of F# and DirectX. 我看过F#和DirectX的演示。

User selects a part of F# code and sends it to F# interactive. 用户选择一部分F#代码并将其发送给F#interactive。 It between, the form's thread is working: the form shows dynamic content and responds 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 ? 我可以得出结论, 对于现有的C#winforms项目,我可以创建一个引用它的F#项目,然后在F#interactive中启动F#项目来运行表单的任意方法吗?

EDIT : what are the steps to invoke btShowNextNumber_Click in a running application ? 编辑 :在正在运行的应用程序中调用btShowNextNumber_Click的步骤是什么?

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: 在F#interactive中,您可以执行以下操作:

#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. 这将打开一个对话框,询问使用哪个调试器。 Eg you can use the same visual studio instance as the one you would use for executing the script. 例如,您可以使用与用于执行脚本的视觉工作室实例相同的视觉工作室实例。

F# is just such CLR language as C# or VB are. F#就像C#或VB那样的CLR语言。 This means that it may reference CLR assemblies (both DLLs and EXEs), import public classes from them and invoke public methods and properties. 这意味着它可以引用CLR程序集(DLL和EXE),从它们导入公共类并调用公共方法和属性。 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 ). 请注意,要使WinForm UI元素响应用户操作,您需要运行Windows消息循环(使用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 ). 在F#shell的情况下,似乎它们在后台线程中运行它并使用Control.Invoke或其等价物( WindowsFormsSynchronizationContext )将操作发布到此线程。

To invoke method you've shown in the sample you need do one of the following: 要调用您在示例中显示的方法,您需要执行以下操作之一:

1) if method btShowNextNumber_Click is public: 1)如果方法btShowNextNumber_Click是公共的:

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: 2)如果方法btShowNextNumber_Click不公开,但Button btShowNextNumber是:

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 . 据我了解F#shell的工作方式,它可以通过SynchronizationContext.Send发布的命令设置透明,就像IronPython shell 一样 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. 我在不同的容器中调试我的代码,如MS Office,IE,Windows资源管理器,Visual Studio和其他VB / C#应用程序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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