Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I execute a script with Roslyn in End User Preview

Tags:

c#

roslyn

I'm trying to play around with the end user preview of roslyn and would like to execute a simple script. What I would like to do is something like:

static void Main(string[] args)
{
    // Is this even valid?
    var myScript = "int x = 5; int y = 6; x + y;";

    // What should I do here?
    var compiledScript = Something.Compile(myScript);
    var result = compiledScript.Execute(myScript);


    Console.WriteLine(result);
}

Can someone point to some resources and/or tell me which nuget packages to install to make this happen. I've installed the Microsoft.CodeAnalysis, but can't figure out if it doable with just that, I feel like I'm missing something.

like image 985
Tomas Jansson Avatar asked May 13 '14 21:05

Tomas Jansson


2 Answers

The scripting APIs which would allow you to do this very easily were (temporarily) removed in the latest preview. You can still compile a script, emit and load the assembly and invoke its entry point by doing something along the lines of

public static class Program
{
    public static void Main(string[] args)
    {
        var assemblyPath = Path.GetDirectoryName(typeof(object).Assembly.Location);

        var defaultReferences = new[] { "mscorlib.dll", "System.dll", "System.Core.dll" };

        var script = @"using System;
        public static class Program
        {
            public static void Main(string[] args)
            {
                Console.WriteLine(""Hello {0}"", args[0]);
            }
        }";

        // Parse the script to a SyntaxTree
        var syntaxTree = CSharpSyntaxTree.ParseText(script);

        // Compile the SyntaxTree to a CSharpCompilation
        var compilation = CSharpCompilation.Create("Script",
            new[] { syntaxTree },
            defaultReferences.Select(x => new MetadataFileReference(Path.Combine(assemblyPath, x))),
            new CSharpCompilationOptions(OutputKind.ConsoleApplication));

        using (var outputStream = new MemoryStream())
        using (var pdbStream = new MemoryStream())
        {
            // Emit assembly to streams.
            var result = compilation.Emit(outputStream, pdbStream: pdbStream);
            if (!result.Success)
            {
                return;
            }

            // Load the emitted assembly.
            var assembly = Assembly.Load(outputStream.ToArray(), pdbStream.ToArray());

            // Invoke the entry point.
            assembly.EntryPoint.Invoke(null, new object[] { new[] { "Tomas" } });
        }
    }
}

It will output Hello Tomas in the console :)

like image 52
khellang Avatar answered Nov 15 '22 09:11

khellang


It appears that in the April 2014 release, scripting has been temporarily removed:

What happened to the REPL and hosting scripting APIs?

The team is reviewing the designs of these components that you saw in previous CTPs, before re-introducing the components again. Currently the team is working on completing the language semantics of interactive/script code.

like image 34
stuartd Avatar answered Nov 15 '22 08:11

stuartd