How do I get started designing and implementing a script interface for my .NET application?

后端 未结 8 767
礼貌的吻别
礼貌的吻别 2021-01-30 13:47

How do I get started designing and implementing a script interface for my .NET application?

There is VSTA (the .NET equivalent of VBA for COM), but as far as I understan

相关标签:
8条回答
  • 2021-01-30 14:02

    For a free, easy to implement .NET scripting language, have a look at C#.

    See my answer to a similar question.

    As for exposing data, seeing how it's a .NET language, you just need to add your program into the assemblies to link into and make the relevant classes public.

    0 讨论(0)
  • 2021-01-30 14:09

    Take a look at PowerShell.

    It allows you to program simple Cmdlets that are scriptable. It also supports hierachical containers (by default you have the file system, the Registry, certificates store, etc.).

    0 讨论(0)
  • 2021-01-30 14:10

    I'm not sure this will cover your needs, but via reflection you can compile C# code and execute it at runtime (example code here).

    So you could write the script in eg. C# and then compile "it on the fly" and run it directly in the context of your application. Of course you have to keep security considerations in mind, but if the scripts are trusted then it might work for you, and you get the benefits of using a powerful managed language for your scripts.

    If you need high performance or running thousands of scripts it might be too slow though.

    0 讨论(0)
  • 2021-01-30 14:12

    I implemented CS-Script as the scripting platform last for a workflow system I wrote. Each wrokflow needed had different conditions that dictated what users would subscribe to various tasks and who would receive emails. With a scripting model it became easy to introduce new steps into the workflows and handle unique requirements that those tasks required.

    The other nice by product of the scripting model was that workflows could be tested under a variety of conditions, and an iterative approach could be taken to finalize the workflow behaviors. During QA and user acceptance testing I included logging functions in the scripts to that I could hunt issues more easily.

    With CS-Script you have complete access to you objects; that is, when your script imports your assemblies you can instantiate your object in your script code. Furthermore, you can retain your compiled scripts and simply supply parameters to them. If your assemblies use a parameters object or a Dictionary, you can pass that your script and execute methods on the objects contained in the parameter object.

    0 讨论(0)
  • 2021-01-30 14:13

    I used CS-Script to create something like you want. In my case I defined an interface in my application. A script then just needed to implement this interface so that it could be run from the application. My application was about processing scanned images and therefore my interface looked something like this:

    public interface ICustomModule
    {
        void ProcessBatch(IBatch batch)
    }
    

    This way my script could access the object model that I defined in my application (in my case through IBatch). The good thing was that during development I could use a normal Class Library project for the script: IntelliSense, debugging... I do not remember the exact details but I basically had a switch in my application that told the app to use the referenced class library instead of a script.

    Furthermore I made an additional interface that allowed the scripts to be configured: A script could define a set of properties which were then displayed by my application in a property grid. I think I used this version here as this seemed a bit more flexible at the time. This way you can not only allow users to configure the script, but you can also provide help in form of descriptions, choices, default values... The property grid is quite extensible: In one case we had a script that would display a special form to define some complex settings.

    Edit: The application of course did not have a reference to "debug" class library. It just need to load the assembly...

    0 讨论(0)
  • 2021-01-30 14:19

    [EDIT: As covered at length in the comments in this, assuming you have a significant need to enable internal scripting where you are hosting snippets or functions someone gives you to customise your app, as opposed to a purely external scenario where one is providing a facaade to allow people to dig stuff out of your app on a more rigid predefined basis]

    IronRuby and IronPython are very neat and appropriate for this (but as the other answer says, PowerShell may be appropriate if you have a more infrastructure-type thing).

    EDIT: Other ideas for enabling internal scripting are

    • using Windows Workflow Foundation (exposing activities to it and/or hosting instances of workflows)
    • using Spring.NET's Expression Language (which is terse, easy to doc and learn but surprisingly powerful)

    EDIT 2 June 2011: IronJS may also be a suitable candidate, there's a Hanselminutes that talks it thru.

    0 讨论(0)
提交回复
热议问题