Create a gist now

Instantly share code, notes, and snippets.

#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