Skip to content

Instantly share code, notes, and snippets.

@svick
Created April 6, 2016 14:37
Show Gist options
  • Select an option

  • Save svick/da22b8f623dd563a2e54ab1110768a57 to your computer and use it in GitHub Desktop.

Select an option

Save svick/da22b8f623dd563a2e54ab1110768a57 to your computer and use it in GitHub Desktop.
using System;
using Microsoft.CodeAnalysis.CSharp.Scripting;
using Microsoft.CodeAnalysis.Scripting;
namespace ConsoleApplication1
{
class Program
{
static void Main()
{
string code = @"
public class class_name
{
private int x;
private int y;
public class_name(int x, int y)
{
this.x = x;
this.y = y;
}
dynamic F1234()
{
return x + y;
}
dynamic F5678()
{
return x;
}
public dynamic Go(string identifer)
{
switch (identifer)
{
case ""F1234"":
return F1234();
case ""F5678"":
return F1234();
default:
throw new System.Exception();
}
}
}
new class_name(X, Y).Go(Identifier)";
var script = CSharpScript.Create<int>(code, globalsType: typeof(Globals),
options: ScriptOptions.Default.WithReferences(
typeof(System.Runtime.CompilerServices.DynamicAttribute).Assembly,
typeof(Microsoft.CSharp.RuntimeBinder.Binder).Assembly));
script.Compile();
Console.WriteLine(script.RunAsync(new Globals { X = 40, Y = 2, Identifier = "F1234" }).Result.ReturnValue);
}
}
public class Globals
{
public int X;
public int Y;
public string Identifier;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment