Home > Net >  Pass array as input parameter in CSharpScript
Pass array as input parameter in CSharpScript

Time:12-05

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.

CodePudding user response:

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

  • Related