Created
May 4, 2016 05:33
-
-
Save guitarrapc/fab39b7ce0534a17c5c11de521313c7e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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