简体   繁体   English

从.Net应用程序获取ScriptCS中的参数

[英]Getting Parameters in ScriptCS from a .Net Application

I have a little beginner problem with ScriptCS and can't find an answer to this. 我有一个ScriptCS的初学者问题,但无法找到答案。 We want to use ScriptCS to dynamically extend a hosted application. 我们希望使用ScriptCS动态扩展托管应用程序。 For this we need to pass parameters into the Script and getting values back from the Script. 为此,我们需要将参数传递给脚本并从脚本中获取值。 I have currently an example console applicatin in which I'm testing the script execution: 我目前有一个示例控制台应用程序,我正在测试脚本执行:

Program.cs Program.cs中

using ScriptCs.Hosting;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ScriptCs.Contracts;
using ScriptCs;
using ScriptCs.Engine.Mono;
using ScriptCs.Engine.Roslyn;
using System.Diagnostics;
namespace ScriptCsPoc
{
    class MainClass
    {
        public static void Main(string[] args)
        {
            var console = (IConsole)new ScriptConsole();
            var logProvider = new ColoredConsoleLogProvider(LogLevel.Info, console);

            var builder = new ScriptServicesBuilder(console, logProvider);

            SetEngine(builder);
            var services = builder.Build();

            var executor = (ScriptExecutor)services.Executor;
            executor.Initialize(Enumerable.Empty<string>(), Enumerable.Empty<IScriptPack>(), "Guten Tag aus der Konsole");
            ExecuteLooseScript(executor);
            ExecuteFile(executor);
            ExecuteFileFromCommandLine();
        }

        public static void ExecuteLooseScript(ScriptExecutor executor)
        {
            var script = @"Console.WriteLine(""Hello from scriptcs"")";
            executor.ExecuteScript(script);
        }

        public static void ExecuteFile(ScriptExecutor executor)
        {

            ScriptResult result = executor.Execute("HelloWorld.csx", "parameter");
            Console.WriteLine("Done Script");
        }


        public static void ExecuteFileFromCommandLine()
        {

            Process cmd = new Process();
            cmd.StartInfo.FileName = "cmd.exe";
            cmd.StartInfo.RedirectStandardInput = true;
            cmd.StartInfo.RedirectStandardOutput = true;
            cmd.StartInfo.CreateNoWindow = true;
            cmd.StartInfo.UseShellExecute = false;
            cmd.Start();

            cmd.StandardInput.WriteLine("scriptcs HelloWorld.csx -- blubber");
            cmd.StandardInput.Flush();
            cmd.StandardInput.Close();
            cmd.WaitForExit();
            string standardOut = cmd.StandardOutput.ReadToEnd();
            Console.WriteLine("Done Script");
        }
        static void SetEngine(ScriptServicesBuilder builder)
        {
            var useMono = Type.GetType("Mono.Runtime") != null;
            if (useMono)
            {
                builder.ScriptEngine<MonoScriptEngine>();
            }
            else {
                builder.ScriptEngine<CSharpScriptEngine>();
            }
        }
    }
}

HelloWorld.csx HelloWorld.csx

#load child/greeter.csx
var greeter = new Greeter();
if (Env.ScriptArgs.Count > 0)
    greeter.Greet(Env.ScriptArgs[0]);
else
    greeter.Greet("Inside Script");

greeter.csx greeter.csx

public class Greeter {
    public void Greet(string message) {
        Console.WriteLine(message);
    }
}

I'm able to call the script within a call of the Console (ExecuteFileFromCommandLine()), but if I'm using an instance of the ScriptExecutor-Class, the passed arguments aren't available in the Env.ScriptArgs. 我可以在Console(ExecuteFileFromCommandLine())的调用中调用脚本,但如果我使用的是ScriptExecutor-Class的实例,则传递的参数在Env.ScriptArgs中不可用。 Does anybody know, how exactly can I get the Parameters from the ScriptExecutor-Class? 有谁知道,我怎样才能从ScriptExecutor-Class中获取参数?

With kind regards 亲切的问候

Okay, the solution to this goes over an additional project: 好的,解决这个问题的方法是另外一个项目:

https://github.com/swn1/scriptcsambient https://github.com/swn1/scriptcsambient

With the Scriptcsambient it is possible to easy give parameters into a ScriptCS-Script and get values back from it. 使用Scriptcsambient,可以轻松地将参数提供给ScriptCS-Script并从中获取值。

With kind regards 亲切的问候

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

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