Use anonymous types as globals in C# scripts
See original GitHub issueDescription In order to be more flexible when passing globals to C# scripts, I like to use anonymous types and objects that can be passed.
Usage Example:
var globals = new
{
Model1 = new SomeBusinessModel("A Role"),
Model2 = new SomeBusinessModel("Another Role")
};
var loader = new InteractiveAssemblyLoader();
var options = ScriptOptions
.Default
.WithMetadataResolver(new CustomMetadataReferenceResolver())
.WithSourceResolver(new CustomSourceReferenceResolver())
.WithFilePath(Environment.CurrentDirectory);
var script = CSharpScript.Create(code, options, globals.GetType(), loader);
var scriptState = await script.RunAsync(globals);
Actual Behavior: Currently, a TypeAccessException is thrown by CSharpScript.RunAsync when an anonymous object is passed. It confuses me, that the CSharpScript.Create method accepts the anonymous type before.
This leads me again to the question: why must the type of globals be defined in the CSharpScript.Create method?
Issue Analytics
- State:
- Created 7 years ago
- Reactions:5
- Comments:5 (2 by maintainers)
Top Results From Across the Web
Build anonymous type from an array of <T>
I am trying to use the new Roslyn scripting modules. This is an example of how to use it. Notice that Globals appears...
Read more >Anonymous Types
Anonymous types in C# encapsulate a set of read-only properties in an object without having to explicitly define a type.
Read more >Global variables for different Script objects
Hi, I'm trying to use MoonSharp to run some functions at same time, ... I'm initializing different Script objects for each lua code....
Read more >Convert Anonymous to Named Type refactoring
Convert anonymous type(s) into a named type · Press Ctrl Shift 0R and then choose Replace Anonymous Type with Named Class. · Right-click...
Read more >Global Variables Are Bad - C2 wiki
If a global variable exists, I would assume that it is used. If it is used, there are methods associated with it. Colocate...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
lol )) Religion does not allow to use php ))
Re globals type: The type must be known so that the compiler knows what external members (members of the globals type) should be available in the top-level scope.