Pass array as input parameter in CSharpScript

Pass array as input parameter in CSharpScript

Problem Description:

I try to run method by CSharpScript.

Method:

public class TaskSolution 
{ 
   public int[] Calculate(int[] inputValue) 
   {
      return inputValue;
   }
}

I tried this solution:

var script = CSharpScript.Create(solution.Code);
var input = new int[3] { 1, 2, 3 };
var call = await script.ContinueWith<int[]>($"new TaskSolution().Calculate({input})").RunAsync();

But it throws Microsoft.CodeAnalysis.Scripting.CompilationErrorException with text "(1,43): error CS0443: Syntax error; value expected" and no more information inside.

When I run similar method but with simple input parameter (as int or string) – it runs successfully. But I meet problems with using arrays.

Solution – 1

$"new TaskSolution().Calculate({input})" evaluates to "new TaskSolution().Calculate(System.Int32[])", which is not valid code. input would be treated as string, not passed as the actual array.

Rate this post
We use cookies in order to give you the best possible experience on our website. By continuing to use this site, you agree to our use of cookies.
Accept
Reject