Skip to content

Instantly share code, notes, and snippets.

@guitarrapc
Created May 4, 2016 05:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save guitarrapc/fab39b7ce0534a17c5c11de521313c7e to your computer and use it in GitHub Desktop.
Save guitarrapc/fab39b7ce0534a17c5c11de521313c7e to your computer and use it in GitHub Desktop.
#r "System.Collections"
#r "System.Runtime"
#r "System.Reflection"
#r "System.Threading.Tasks"
#r "System.Net.Http"
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Reflection;
using System.Threading.Tasks;
using Newtonsoft.Json;
using Microsoft.CodeAnalysis.Scripting;
using Microsoft.CodeAnalysis.CSharp.Scripting;
private static readonly string[] DefaultImports =
{
"System",
"System.IO",
"System.Linq",
"System.Collections.Generic",
"System.Text",
"System.Text.RegularExpressions",
"System.Net",
"System.Net.Http",
"System.Threading",
"System.Threading.Tasks",
};
private static readonly Assembly[] DefaultReferences =
{
typeof(Enumerable).Assembly,
typeof(List<string>).Assembly,
typeof(System.Net.Http.HttpClient).Assembly,
};
public static async Task<string> EvaluateCSharpAsync(string code)
{
object result = null;
try
{
result = await CSharpScript.EvaluateAsync(code ?? "コードが空ですよ?",
ScriptOptions.Default
.WithImports(DefaultImports)
.WithReferences(new [] {
"System",
"System.Core",
"System.Xml",
"System.Xml.Linq",
})
.WithReferences(DefaultReferences));
}
catch (Exception ex)
{
result = $"{ex.Message}{Environment.NewLine}{ex.StackTrace}";
}
var resultText = result?.ToString() ?? "";
return resultText;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment