This project is read-only.

How to compile a script of code at runtime and call it immediately?

Topics: APIs
Apr 10, 2014 at 12:24 PM
Edited Apr 10, 2014 at 12:26 PM
Hello team,
First of all I have to say that you have done amazing and fantastic work.
In previous versions we could make a call like this one:
MyClass c = new MyClass {Name = "Abolfazl"};
Session session = Session.Create(c);
var engine = new ScriptEngine(new[] { c.GetType().Assembly.Location });

engine.Execute("var a = 5;var b = new testDynamic.MyClass();");
var entity1 = engine.Execute("new { FirstName = Name, LastName= \"Hosnoddin\" }", session);
How to compile code at runtime like this one with new API and new Model?
I've seen your FAQ samples and how to Emit a Compilation as assembly file, but what about dynamic calls?

Best Regards
Abolfazl
Apr 10, 2014 at 2:59 PM
From the FAQ:

What happened to the REPL and hosting scripting APIs?

The team is reviewing the designs of these components that you saw in previous CTPs, before re-introducing the components again. Currently the team is working on completing the language semantics of interactive/script code.
So, there is no easy way to do what you want with the current version of Roslyn. Hopefully, that will change soon.
Marked as answer by TomasMatousek on 4/18/2014 at 8:54 PM
May 26, 2014 at 10:14 PM
Awwwwwww dang... I just spent a couple hours working on a new project that I planned on using the ScriptEngine in and, now that I'm ready to implement that part of it, I find out that it's gone.
Jun 13, 2014 at 9:54 PM
Edited Jun 13, 2014 at 9:55 PM
I would like to see this feature back in Roslyn as well. I am currently using the old CTP for this functionality.

One thing that I noticed was that the first time Session.Execute() is called there is a HUGE amount of time spent executing the statement, no matter how simple of a statement it is... ie:
session.Execute("var i = 0;");
session.Execute("var json = JsonConvert.Deserialize("{blah: 'blah'}");
the first statement, even though it should take significantly less time to execute actually takes MORE time. Is this because the referenced assemblies are loaded during the first Execute()? Is that being changed so that we can load them beforehand? ...like when the scripting engine is created?

Thanks,
Rush
Jul 8, 2014 at 10:41 AM
Yes. The ScriptEngine is very important for me too. I am writing a project where I want to use dynamic objects, and the old CTP does not understand dynamic correctly.
Please !
Sep 4, 2014 at 11:16 PM
When is the ScriptEngine going to be returned? Without it there is no point to Roslyn. It's the reason for me to even look at Roslyn. Not syntax highlighting and code analysis. It's like having dry water.
Sep 5, 2014 at 12:28 PM
The scripting features are very important to me, as well. I would like someone on the Roslyn team provide a brief overview of this feature and what's to come (e.g. comparison to older previews, new features, possibilities, etc.). I completely understand priorities, timing, releases, etc. However, being transparent on such a huge capability coming to the language and tooling would be great. I'm afraid (based on the silence) that scripting & the Interactive Window may not make it in the next release. I still want to give praise on all the other work from the teams and new CTPs as the new compiler platform is a huge undertaking. Just my 2 pennies. Thanks
Sep 5, 2014 at 1:41 PM
Edited Sep 5, 2014 at 1:41 PM
I'd like to know what the problem actually is. Why doesn't scripting work with Roslyn (allegedly a CLR issue), while the Mono C# compiler and the Microsoft F# compiler seem to support scripting without any issues?
Sep 5, 2014 at 2:24 PM
@Expandable From my understanding they did not work on the scripting api in order to concentrate on the higher priority items and because it was out of date at the time of the last release they dropped it from the library. I have a feeling it will return in the next release though. One workaround for the time being is to compile the "script" code and use reflection to execute it. That can be done all in memory.
Sep 5, 2014 at 2:31 PM
rushfrisby wrote:
@Expandable From my understanding they did not work on the scripting api in order to concentrate on the higher priority items and because it was out of date at the time of the last release they dropped it from the library.
True, but to be really useful, you probably also want to use that with collectible assemblies; see also this thread:

TomasMatousek wrote:
We have removed the support for collectible assemblies since the underlying implementation in the CLR doesn't provide us the capabilities we need to build a fully functional C#/VB code gen on top of it. The implementation we had was mostly experimental with a lot of workarounds and problems.
On the other hand, maybe Mono's C# compiler and the F# compiler don't use collectible assemblies for the same reasons?
Sep 6, 2014 at 8:42 PM
shaggygi97 wrote:
The scripting features are very important to me, as well. I would like someone on the Roslyn team provide a brief overview of this feature and what's to come (e.g. comparison to older previews, new features, possibilities, etc.). I completely understand priorities, timing, releases, etc. However, being transparent on such a huge capability coming to the language and tooling would be great. I'm afraid (based on the silence) that scripting & the Interactive Window may not make it in the next release. I still want to give praise on all the other work from the teams and new CTPs as the new compiler platform is a huge undertaking. Just my 2 pennies. Thanks
Hey shaggygi97,

The scripting engine doesn't have any dependencies on Visual Studio and so our timeline for releasing it isn't directly related to the release schedule for Visual Studio; it can't "not make it in the next release" per se. There's a lot of interest in building great things on top of this key component both internally and externally (not least of which is the Interactive Window) and we're anxious to get the next revision of it into your hands.

That said, it's important to understand that every language feature may require work to make sure the semantics work properly in a script. And that also cascades out into the Interactive window. For example, C# dynamic and Async/await require unique scripting support and their addition destabilized the scripting engine and the Interactive Window.

Additionally, there is a proposed change to the shape of the scripting API to make using it for the simple scenarios wonderfully concise. We're still evaluating whether the impact of moving to that shape and ensuring it still supports the breadth of advanced scenarios we know of.

Consequently, our current priorities are on finalizing and stabilizing the feature set of C# 6 and VB 14 and finalizing our APIs. Once that's done we can properly work on scripting support without ... chasing a moving train, if that makes sense?

Regards,

Anthony D. Green, Program Manager, Visual Basic & C# Languages Team
Sep 6, 2014 at 9:28 PM
@ADGreen
Thanks so much for an update. I follow exactly what you stated. As mentioned before, I understand priorities and it takes time to get things out... even if it is the features I'm really looking forward to. The async/await feature in the first preview scripting API was the language feature I really wanted to test out with one of my APIs and understand the complexities.

I want to make sure the Roslyn team does think we're just ranting because things take time and not available for us today... as they always do in our coding worlds :)

One last suggestion/request... I hope the team releases some good walkthroughs similar to the other code examples when the scripting APIs roll out. That would be a big help for us to understand the changes compared to the original 2012 CTP.

Again, keep up the good work and I hope to see the Interactive Window and scripting features in the not-so-distant future.
Sep 17, 2014 at 12:45 AM
I would like to just add to this mix and say that I fell into the same "trap" as at least one other person posting in this thread. Had actually setup a complete system, downloaded VS2014 CPT3, installed SDK, installed templates, cloned the repository, built it... and was like ?????!?!? Where did Roslyn.Scripting.CSharp and all commonly found libraries in most all examples one finds about this run off to ???!?!?!?
Then I found this thread... and am I to guess that this BEAUTIFUL feature was tanked due to an exec's desire to see VS2014 have better tool capabilities??
:( Sad...on two counts:
1.) The fact that the "updated" version of Roslyn is literally borked if one wanted to use the C# "runtime" scripting capabilities...
2.) There is no real mention of this in any "well defined" area...

Being able to "script" C# or rather...compile and execute at runtime is a huge thing...IMO it is bigger than any IDE tool/plugin/feature that anyone could possibly make...and if it came from Microsoft one would expect that it would be done in an very "smart" and (within given time constraints) efficient manner.

sigh...spent almost 12 hours prepping, researching, and then trying to figure out where this "ScriptEngine" existed... the source appears to still be there...just no real reference to it in the updated version's project/solution.

All of the other features I am sure have great power and probably it is my lack of understanding as to why they were put ahead of something as cool as the ScriptEngine...

Well... I guess I could always roll back to the earlier CPT, use my VS2012, and build some libraries that I can hook into.

Don't get me wrong... my sadness is only in the fact that it got put to the wayside...obviously you guys have some very cool stuff going on right now...and probably many other "fires" that are a bit brighter on your radar than the ScriptEngine...

But for what it is worth... when there is the time... oh please please please...bring it back...and keep it in the mix...

So many cool things could be done with this...

p.s. Maybe post a blurb on the home page of this project that the ScriptEngine was removed/does not work (with maybe a "coming later...or...coming eventually") with the most recent Roslyn CPT...might save other folks who followed the same path as I did the time from coming to this conclusion.

Cheers!
Dec 30, 2014 at 9:42 AM
I have created a project that uses Roslyn. And because the ScriptEngine is not anymore in the last version of Roslyn, I use Visual 2012. The project is destinated to show-control programmers, it is there: https://showcontrolnet.codeplex.com/
Dec 30, 2014 at 11:47 AM
yanterrien wrote:
I have created a project that uses Roslyn. And because the ScriptEngine is not anymore in the last version of Roslyn, I use Visual 2012. The project is destinated to show-control programmers, it is there: https://showcontrolnet.codeplex.com/
Don't you mean Visual Studio 2013?
Dec 30, 2014 at 1:22 PM
PauloMorgado wrote:
yanterrien wrote:
I have created a project that uses Roslyn. And because the ScriptEngine is not anymore in the last version of Roslyn, I use Visual 2012. The project is destinated to show-control programmers, it is there: https://showcontrolnet.codeplex.com/
Don't you mean Visual Studio 2013?
No, my project uses Visual Studio 2012. Why do you ask ?
Dec 30, 2014 at 1:45 PM
yanterrien wrote:
PauloMorgado wrote:
yanterrien wrote:
I have created a project that uses Roslyn. And because the ScriptEngine is not anymore in the last version of Roslyn, I use Visual 2012. The project is destinated to show-control programmers, it is there: https://showcontrolnet.codeplex.com/
Don't you mean Visual Studio 2013?
No, my project uses Visual Studio 2012. Why do you ask ?
Just checking.
Feb 14, 2015 at 4:29 PM
So is there an ETA for when this feature will be added back in? I built a fair amount of software that depends on ScriptEngine with the assumption that it would remain there, and its removal is somewhat problematic for me.

Will this appear in the final VS2015 release, or is it slated for a date after that?
Feb 14, 2015 at 4:57 PM
The updated scripting APIs are now back in the github repo. I would navigate there to review. It is not in the current ctp and MS has stated the scripting and interactive repl will not be included out of box for vs15. However, will be released soon after rtm. Of course things could change. Hope this helps.