0

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?

1 Answer 1

2

As the error is trying to tell you, you can't put a semicolon after a method declaration.

You can see this clearly here

Delete the semicolon and you'll be fine.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.