首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >向现有C#项目添加脚本功能

向现有C#项目添加脚本功能
EN

Stack Overflow用户
提问于 2016-11-12 00:56:51
回答 2查看 373关注 0票数 1

我在一个研究小组工作,我的任务是在数据采集程序中添加脚本功能。理想情况下,我希望能够在数据采集软件运行时编写脚本(并将这些脚本保存为运行中的文件)。命令行也可能更好。

我对C#一点经验都没有,但我确实有相当多的其他语言(Objective-C,Python)的编程经验。我看到了这个链接https://blogs.msdn.microsoft.com/cdndevs/2015/12/01/adding-c-scripting-to-your-development-arsenal-part-1/,它详细介绍了"Roselyn脚本包“,但我不确定这是否是我最好的选择。

有谁能推荐获得完整脚本功能的最简单的方法吗?(我正在努力避免在这里失去我生命中的几个月=p)非常感谢链接开始于/advice。

谢谢!

EN

回答 2

Stack Overflow用户

发布于 2016-11-12 02:06:51

你为我发布了一个有趣的链接,因为我几周前刚刚制作了类似的原型,但还没有实现。我的目标是在网页上创建一个C#“即时”控制台。

有一些关于以编程方式加载某些程序集的小问题,并且必须显式引用它们。

这里是代码背后,请稍后张贴您的解决方案,我有兴趣知道。

这样就可以在运行时编写c#代码,还可以得到一个字符串返回。

代码语言:javascript
复制
protected void getImmediateResult_Click(object sender, EventArgs e)
    {

        //building the code
        string source = @"using System; 
        class MyType{
        public static String Evaluate(){
        <!expression!>
        }}";

        string expression = this.txtimmediate.Text;
        string finalSource = source.Replace("<!expression!>", expression);
        textcodeCheck.Text = finalSource;
        var compileUnit = new CodeSnippetCompileUnit(finalSource);

        //preparing compilation
        CodeDomProvider provider = new Microsoft.CSharp.CSharpCodeProvider();

        // Create the optional compiler parameters

        //this correctly references the application but no System.Web etc
        string[] refArray = new string[2];

        UriBuilder uri = new UriBuilder(Assembly.GetExecutingAssembly().CodeBase);
        refArray[0] = uri.Path;

        //this works
        refArray[1] = "System.Web" + ".dll";

        ////NOT WORKING for non microsoft assemblies
        //var allRefs = Assembly.GetExecutingAssembly().GetReferencedAssemblies();
        //string[] refArray = new string[allRefs.Length + 1];
        //int i = 1;
        //foreach (AssemblyName refer in allRefs)
        //{
        //    refArray[i] = refer.Name + ".dll";
        //    i++;
        //}

        var compilerParameters = new CompilerParameters(refArray);

        CompilerResults compilerResults = provider.CompileAssemblyFromDom(compilerParameters, compileUnit);
        if (compilerResults.Errors.Count > 0)
        {
            //1st error
            this.txtResult.Text = compilerResults.Errors[0].ErrorText;
            return;
        }

        //running it
        Type type = compilerResults.CompiledAssembly.GetType("MyType");
        MethodInfo method = type.GetMethod("Evaluate");
        String result = (String)method.Invoke(null, null);


        this.txtResult.Text = result;
    }
票数 0
EN

Stack Overflow用户

发布于 2016-11-12 05:57:46

如果您愿意使用IronPython,您可以直接在C#中执行脚本:

代码语言:javascript
复制
using IronPython.Hosting;
using Microsoft.Scripting.Hosting;

private static void doPython()
{
    ScriptEngine engine = Python.CreateEngine();
    engine.ExecuteFile(@"test.py");
}

Get IronPython here.

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40552714

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档