question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Use anonymous types as globals in C# scripts

See original GitHub issue

Description 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:open
  • Created 7 years ago
  • Reactions:5
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
Diaskhancommented, Jan 9, 2019

lol )) Religion does not allow to use php ))

1reaction
tmatcommented, Oct 23, 2016

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found