Pass array as input parameter in CSharpScript

120 views Asked by At

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.

1

There are 1 answers

1
Sohaib Jundi On BEST ANSWER

$"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.