roslyn: Cannot use ExpandoObject as 'globals' parameter for Script.Run()
How to reproduce:
var script = CSharpScript.Create("a + b");
dynamic expando = new ExpandoObject();
var dictionary = (IDictionary<String, Object>) expando;
dictionary.Add("a", 3);
dictionary.Add("b", 4);
script.Run(expando);
Gives error message:
The name 'b' does not exist in the current context
Is there another, recommended way of giving a dynamically built object as the globals parameter to the script?
About this issue
- Original URL
- State: open
- Created 9 years ago
- Reactions: 16
- Comments: 29 (8 by maintainers)
Also looking for a status update? I’ve implemented the workaround but would prefer not having to access ‘globals’ from another member.
Issue opened on 30 May 2015. We are on Dec 2021 and still no fix. What a shame.
Why Roslyn is not supporting ExpandoObject or Anonymous. Is there any reason?
Still waiting for the fix in the 2022.
Just tried to use ExpandoObject today and it appears this is still not supported. However as I still need to accept on-demand globals just before the script is executed, I am currently trying the following workaround:
test
, the generated script would beint test = default(int);
When running the above example I get
which is in Microsoft.CSharp.dll Did this happen to anyone else?
Any update on this issue, or is it still on the backlog? edit: solved our needs with the example below
No update, it’s still on backlog.
An alternative workaround would be to add an indirection:
The cause of this not being supported can be found in https://github.com/dotnet/roslyn/blob/03e30451ce7eb518e364b5806c524623424103e4/src/Scripting/Core/ScriptVariables.cs#L98 where it finds the fields and properties of the given instance and converts it into a map of script variables.
If this could check if the object is an IDictionary<String, Object> object, then my scenario could be supported quite easily (in my opinion, at least).
Or am I completely off-base? I don’t know the codebase well-enough to actually be confident.