代码之家  ›  专栏  ›  技术社区  ›  Ewan Makepeace

嵌入C#桌面应用程序的最佳脚本语言是什么?[闭门]

  •  94
  • Ewan Makepeace  · 技术社区  · 17 年前

    我们正在编写一个复杂的富桌面应用程序,需要在报告格式上提供灵活性,因此我们认为只需将对象模型公开给脚本语言即可。曾经,这意味着VBA(仍然是一种选择),但托管代码派生的VSTA(我认为)似乎已经枯萎了。

    对于Windows.NET上的嵌入式脚本语言,现在的最佳选择是什么?

    16 回复  |  直到 16 年前
        1
  •  114
  •   Peter Mortensen Pieter Jan Bonestroo    16 年前

    就我个人而言,我会使用C#作为脚本语言。NET框架(以及Mono,感谢Matthew Scharley)实际上在框架本身中包含了每种.NET语言的编译器。

    基本上,这个系统的实现分为两个部分。

    1. 允许用户编译代码

    2. 创建和使用编译程序集中包含的类 这比上一步稍微困难一点(需要一点反射)。基本上,您应该将编译后的程序集视为程序的“插件”。有很多教程介绍了在C#中创建插件系统的各种方法(Google是您的朋友)。

    在这一点上,我必须为我将要粘贴的大量代码道歉(我本来不想让它这么大,但我的评论有点过火了)

    
    using System;
    using System.Windows.Forms;
    using System.Reflection;
    using System.CodeDom.Compiler;
    
    namespace ScriptingInterface
    {
        public interface IScriptType1
        {
            string RunScript(int value);
        }
    }
    
    namespace ScriptingExample
    {
        static class Program
        {
            /// 
            /// The main entry point for the application.
            /// 
            [STAThread]
            static void Main()
            {
    
                // Lets compile some code (I'm lazy, so I'll just hardcode it all, i'm sure you can work out how to read from a file/text box instead
                Assembly compiledScript = CompileCode(
                    "namespace SimpleScripts" +
                    "{" +
                    "    public class MyScriptMul5 : ScriptingInterface.IScriptType1" +
                    "    {" +
                    "        public string RunScript(int value)" +
                    "        {" +
                    "            return this.ToString() + \" just ran! Result: \" + (value*5).ToString();" +
                    "        }" +
                    "    }" +
                    "    public class MyScriptNegate : ScriptingInterface.IScriptType1" +
                    "    {" +
                    "        public string RunScript(int value)" +
                    "        {" +
                    "            return this.ToString() + \" just ran! Result: \" + (-value).ToString();" +
                    "        }" +
                    "    }" +
                    "}");
    
                if (compiledScript != null)
                {
                    RunScript(compiledScript);
                }
            }
    
            static Assembly CompileCode(string code)
            {
                // Create a code provider
                // This class implements the 'CodeDomProvider' class as its base. All of the current .Net languages (at least Microsoft ones)
                // come with thier own implemtation, thus you can allow the user to use the language of thier choice (though i recommend that
                // you don't allow the use of c++, which is too volatile for scripting use - memory leaks anyone?)
                Microsoft.CSharp.CSharpCodeProvider csProvider = new Microsoft.CSharp.CSharpCodeProvider();
    
                // Setup our options
                CompilerParameters options = new CompilerParameters();
                options.GenerateExecutable = false; // we want a Dll (or "Class Library" as its called in .Net)
                options.GenerateInMemory = true; // Saves us from deleting the Dll when we are done with it, though you could set this to false and save start-up time by next time by not having to re-compile
                // And set any others you want, there a quite a few, take some time to look through them all and decide which fit your application best!
    
                // Add any references you want the users to be able to access, be warned that giving them access to some classes can allow
                // harmful code to be written and executed. I recommend that you write your own Class library that is the only reference it allows
                // thus they can only do the things you want them to.
                // (though things like "System.Xml.dll" can be useful, just need to provide a way users can read a file to pass in to it)
                // Just to avoid bloatin this example to much, we will just add THIS program to its references, that way we don't need another
                // project to store the interfaces that both this class and the other uses. Just remember, this will expose ALL public classes to
                // the "script"
                options.ReferencedAssemblies.Add(Assembly.GetExecutingAssembly().Location);
    
                // Compile our code
                CompilerResults result;
                result = csProvider.CompileAssemblyFromSource(options, code);
    
                if (result.Errors.HasErrors)
                {
                    // TODO: report back to the user that the script has errored
                    return null;
                }
    
                if (result.Errors.HasWarnings)
                {
                    // TODO: tell the user about the warnings, might want to prompt them if they want to continue
                    // runnning the "script"
                }
    
                return result.CompiledAssembly;
            }
    
            static void RunScript(Assembly script)
            {
                // Now that we have a compiled script, lets run them
                foreach (Type type in script.GetExportedTypes())
                {
                    foreach (Type iface in type.GetInterfaces())
                    {
                        if (iface == typeof(ScriptingInterface.IScriptType1))
                        {
                            // yay, we found a script interface, lets create it and run it!
    
                            // Get the constructor for the current type
                            // you can also specify what creation parameter types you want to pass to it,
                            // so you could possibly pass in data it might need, or a class that it can use to query the host application
                            ConstructorInfo constructor = type.GetConstructor(System.Type.EmptyTypes);
                            if (constructor != null && constructor.IsPublic)
                            {
                                // lets be friendly and only do things legitimitely by only using valid constructors
    
                                // we specified that we wanted a constructor that doesn't take parameters, so don't pass parameters
                                ScriptingInterface.IScriptType1 scriptObject = constructor.Invoke(null) as ScriptingInterface.IScriptType1;
                                if (scriptObject != null)
                                {
                                    //Lets run our script and display its results
                                    MessageBox.Show(scriptObject.RunScript(50));
                                }
                                else
                                {
                                    // hmmm, for some reason it didn't create the object
                                    // this shouldn't happen, as we have been doing checks all along, but we should
                                    // inform the user something bad has happened, and possibly request them to send
                                    // you the script so you can debug this problem
                                }
                            }
                            else
                            {
                                // and even more friendly and explain that there was no valid constructor
                                // found and thats why this script object wasn't run
                            }
                        }
                    }
                }
            }
        }
    }
    
    
        2
  •  36
  •   Ben Hoffstein    17 年前
        3
  •  24
  •   Hector Sosa Jr    17 年前

    我用过 CSScript 结果惊人。它真的减少了我在可编写脚本的应用程序中进行绑定和其他低级操作的工作量。

        4
  •  20
  •   Soestae Rodrick Chapman    7 年前

    PowerShell引擎设计为可以轻松嵌入到应用程序中,使其可编写脚本。事实上,PowerShell CLI只是引擎的一个基于文本的接口。

    编辑:参见 https://devblogs.microsoft.com/powershell/making-applications-scriptable-via-powershell/

        5
  •  12
  •   jop    17 年前

    Boo 语言

        6
  •  8
  •   Remo.D    17 年前

    我选择的脚本语言是 Lua 这些天。它体积小、速度快、干净、有完整的文档记录、支持良好,具有很好的应用前景 community ,它被世界上许多大公司使用 industry

    将其与.NET语言一起使用 LuaInterface 该项目将提供所有您需要的。

        7
  •  3
  •   DAC    17 年前

    为什么不试试C#?Mono有一个很棒的新项目,特别是动态评估C#:

    http://tirania.org/blog/archive/2008/Sep-10.html

        8
  •  2
  •   Borek Bernard    17 年前

    如上所述的IronRuby。作为一名C#程序员,我有一个有趣的项目是 C# Eval support in Mono . 但它还不可用(将成为Mono 2.2的一部分)。

        9
  •  2
  •   Robert Rossney    17 年前

        10
  •  1
  •   Peter    13 年前

    S# 这是我目前维持的。这是一个开源项目,用C#编写,专为.NET应用程序设计。

    最初(2007-2009年)举办于 http://www.codeplex.com/scriptdotnet ,但最近它被移到了github。

        11
  •  1
  •   Алексей Богатырев    12 年前

    尝试 Ela . 这是一种类似于Haskell的函数式语言,可以 embedded 进入任何.Net应用程序。即使它有简单但可用的IDE。

        12
  •  0
  •   mattlant    17 年前

    我还没试过这个,但看起来很酷:

    http://www.codeplex.com/scriptdotnet

        13
  •  0
  •   mark mark    17 年前

    我刚刚为一个客户端创建了一个插件,允许他们在模块中编写C代码,就像VBA在Office中所做的那样。

        14
  •  0
  •   Tony    16 年前

    Lua 之前在Delphi应用程序中,但它可以嵌入到很多东西中。它被用在 Adobe's Photoshop Lightroom .

        15
  •  0
  •   lukebuehler    13 年前

    我喜欢用C#本身编写脚本 . 现在,在2013年,对C#脚本的支持非常好,越来越多的库开始提供。

    script C# code ,您可以将它与.NET一起使用,只需包含 Mono.CSharp.dll 在您的应用程序中。对于我制作的C#脚本应用程序,请签出 CShell

    Roslyn 这是微软的,但这只是CTP。

    正如一些人已经提到的, CS-Script