Reputation: 2320
I developed a method to test if a script have bugs:
public static object Test(string code, string references)
{
try
{
Compilation compilation = CSharpScript.Create(code,
options: ScriptOptions.Default
.AddReferences(references)
.AddImports("System.Collections.Specialized", "System.Linq", "System.Net"),
globalsType: typeof(ScriptObject)
).GetCompilation();
using (var ms = new MemoryStream())
{
EmitResult result = compilation.Emit(ms);
if (!result.Success)
{
var failures = result.Diagnostics.Where(diagnostic =>
diagnostic.IsWarningAsError ||
diagnostic.Severity == DiagnosticSeverity.Error).Select(s => s.GetMessage());
return (new { Success = false, ErrorMessage = failures });
}
}
}
catch (Exception e)
{
return (new { Success = false, ErrorMessage = e.Message });
}
return (new { Success = true });
}
If I ran simple code the test pass OK
But if I add to the code a method/function, I get an exception. Ex:
int Add(int x, int y) {
return x+y;
};
Add(1, 4)
Taken from here: https://blogs.msdn.microsoft.com/cdndevs/2015/12/01/adding-c-scripting-to-your-development-arsenal-part-1/
I get the error
; expected,Semicolon after method or accessor block is not valid,Only assignment, call, increment, decrement, and new object expressions can be used as a statement
The error is in the "return x+y;" sentence, if I add "int c = x + y;" I get the error on that line
It's expected to work, isn't it?
Upvotes: 0
Views: 968