Scenario:
C#
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.
@tmat moving to 1.2
Also affects usability of scripting API from within the REPL:
``` C#
public class Globals
. {
. public int X;
. public int Y;
. }
var globals = new Globals { X = 1, Y = 2 };
await CSharpScript.EvaluateAsync("X+Y", globals: globals)
Microsoft.CodeAnalysis.Scripting.CompilationErrorException: (1,3): error CS7012: The name 'Y' does not exist in the current context
```
Any update on when this may be fixed?
@hawkerm We do not have any specific date. We review and re-prioritize open issues regularly for each release. Unfortunately, we have had other work that was deemed more important than this one.
Are you blocked or can you find a workaround for your scenario?
@tmat I'm testing out using Roslyn in a UWP application, but I think with all the security restrictions still it's not going to work out. That's why I was hoping this approach with the Globals would work, as then I could stick in my own object for externalization within the script code being run dynamically.
I think I'm going to have to use something like DynamicExpresso which is purely interpretive and doesn't need to write out a dll, but they don't have full language support yet.
@hawkerm Scripting is not gonna work at all in UWP as UWP doesn't support runtime code generation. Scripting is only supported on .NET Framework and .NET Core.
Same error but for a more global issue for developers working with Entity Framework (.NET Fx) and Roslyn. I got an object in a database I retrieves with EF, and I want to evaluate a condition based on the properties of the object in globals.
error CS7012: The name 'FirstName' does not exist in the current context (are you missing a reference to assembly 'EntityFrameworkDynamicProxies-Kelios.CAM.Engagement.Core, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'?)
The same code works well in my unit tests because I use the business models but it's not working with proxies generated by EF. Is there a way to do it without disabling EF proxy and make my code a nightmare to manage ?
@jchable Roslyn doesn't support compiling directly against assemblies generated by Reflection.Emit. I'm not sure if that's how EF produces the dynamic proxy assembly, but my guess would be so.
Could you share a snippet of code that that demonstrates the issue? Perhaps you could use "dynamic" to access the properties of the object.
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.
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
PHP is more fleunt is this case !
Roslyn lose to php in this use case !
to saaaaaddd
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?
the other solution is to use other experssion libraries.
David has provided some list of it !
https://github.com/davideicardi/DynamicExpresso#other-resources-or-similar-projects
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?
Most helpful comment
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.