I am trying to use the new Roslyn
scripting modules. This is an example of how to use it. Notice that Globals
appears to need to be a class
.
public class Globals
{
public int X;
public int Y;
}
var globals = new Globals { X = 1, Y = 2 };
Console.WriteLine(await CSharpScript.EvaluateAsync<int>("X+Y", globals: globals));
I have a generic function
that takes a type T
, with the length of the array indeterminate (but relatively small length in most cases):
void Func<T>()
{
T[] values;
}
How do I convert the T[]
to an anonymous type
?
So if I have T
if of type decimal
and in this case values
is of length 3,
values[0] = 124.3, values[1] = 132.4, values[2] = 23
I would like to have an anonymous type
created that looks something like this:
var v = new { v1 = 124.3, v2 = 232.4, v3 = 23 };
Is this possible? That is, to create an anonymous type from an array that you don't know the length of at compile time?
NOTE: This is why I need an anonymous type and not a tuple, or a List etc. And, since I don't know how big the array is, I can't hard wire a class
Edit 1
I was somewhat shocked when I tried the solution given below that this even compiles:
dynamic v = new ExpandoObject();
var dictionary = (IDictionary<string, object>)v;
dictionary.Add("X", 1.5);
dictionary.Add("Y", 2.7);
//var globals = new Globals { X = 1.5, Y = 2.7 };
var retval = CSharpScript.EvaluateAsync<double>("System.Math.Sqrt(System.Math.Log(X + Y))",
globals: dictionary).GetAwaiter();
//retval = (decimal)Convert.ChangeType(retval, typeof(decimal));
Console.WriteLine(retval.GetResult());
Sadly, I get a runtime error:
Microsoft.CodeAnalysis.Scripting.CompilationErrorException
HResult=0x80131500
Message=(1,34): error CS0103: The name 'X' does not exist in the current context
Source=Microsoft.CodeAnalysis.Scripting
StackTrace:
at Microsoft.CodeAnalysis.Scripting.ScriptBuilder.ThrowIfAnyCompilationErrors(DiagnosticBag diagnostics, DiagnosticFormatter formatter)
at Microsoft.CodeAnalysis.Scripting.ScriptBuilder.CreateExecutor[T](ScriptCompiler compiler, Compilation compilation, Boolean emitDebugInformation, CancellationToken cancellationToken)
at Microsoft.CodeAnalysis.Scripting.Script`1.GetExecutor(CancellationToken cancellationToken)
at Microsoft.CodeAnalysis.Scripting.Script`1.RunAsync(Object globals, Func`2 catchException, CancellationToken cancellationToken)
at Microsoft.CodeAnalysis.CSharp.Scripting.CSharpScript.RunAsync[T](String code, ScriptOptions options, Object globals, Type globalsType, CancellationToken cancellationToken)
at Microsoft.CodeAnalysis.CSharp.Scripting.CSharpScript.EvaluateAsync[T](String code, ScriptOptions options, Object globals, Type globalsType, CancellationToken cancellationToken)
at Trady.Form1.InitializeScriptEngineAsync() in C:\Users\idf\Form1.cs:line 79
at Form1..ctor() in C:\Users\idf\Form1.cs:line 56
at Trady.Program.Main() in C:\Users\idf\Program.cs:line 19