roslyn: Scripting API: metadata references not backed by a file don't load

Scenario:

var s = await CSharpScript.EvaluateAsync("new MyLib.Class()", ScriptOptions.Default.AddReferences(
   MetadataReference.CreateFromImage(File.ReadAllBytes(@"file.dll"))))

fails since the in-memory assembly is not registered with InteractiveAssemblyLoader.

About this issue

  • Original URL
  • State: open
  • Created 9 years ago
  • Reactions: 9
  • Comments: 16 (5 by maintainers)

Commits related to this issue

Most upvoted comments

Kinda sad that this issue hasn’t been touched in 7 years with no visible change on that matter to come. That’s the first usage scenario that comes to my mind when using scripts: Passing in a globals object, that is not necessarily inside a physical assembly. Takes a lot of the “dynamic” out of scripting.

This is a bummer. I was really excited about using Roslyn as a scripting engine but this limits the use case drastically as I understand it. To only be able to use a predefined class to pass in variables to a script would mean that we would have to have some sort of understanding about what variables are required by the script itself. The whole idea around a scripting engine would be to have the ability to pass in dynamic variables at runtime. I’ve come up with a pretty ugly solution but I feel this should be handled OOB for sure.

I can’t believe the dynamic type isn’t even supported 😦

dynamic globals = new ExpandoObject();
globals.X = 1;
globals.Y = 2;
Console.WriteLine(await CSharpScript.EvaluateAsync<int>("X+Y", globals: globals));
// (1,1): error CS0103: The name 'X' does not exist in the current context

Anonymous types aren’t supported either

var globals = new { X = 1, Y = 2 };
Console.WriteLine(await CSharpScript.EvaluateAsync<int>("X+Y", globals: globals));
//(1,1): error CS0122: '<>f__AnonymousType0<int, int>.X' is inaccessible due to its protection level

Has anyone found any kind of work-around to being required to use static predefined types to pass data to these dynamic scripts?

Is any progress on supporting ExpandoObject or Anonymous?

I can’t believe the dynamic type isn’t even supported 😦

dynamic globals = new ExpandoObject();
globals.X = 1;
globals.Y = 2;
Console.WriteLine(await CSharpScript.EvaluateAsync<int>("X+Y", globals: globals));
// (1,1): error CS0103: The name 'X' does not exist in the current context

Anonymous types aren’t supported either

var globals = new { X = 1, Y = 2 };
Console.WriteLine(await CSharpScript.EvaluateAsync<int>("X+Y", globals: globals));
//(1,1): error CS0122: '<>f__AnonymousType0<int, int>.X' is inaccessible due to its protection level

Has anyone found any kind of work-around to being required to use static predefined types to pass data to these dynamic scripts?

For fleunt implementation it must be dynamic object with variables or just wtih simple properpties

globals =new Object(){ x=null, y=null}; await CSharpScript.EvaluateAsync<int>(“X+Y”, globals: globals)

PHP is more fleunt is this case ! Roslyn lose to php in this use case ! to saaaaaddd