views:

542

answers:

4

What is the best practice for using C# as an embedded internal scripting application for a .NET 3.5 application? I have an app with a few small IronRuby scripts in it. None of which is really exploiting the dynamic nature of IronRuby.

Apparently its against our corporate standard to be using IronRuby or IronPython right now. Ooopps. What is the best way that I can use C# as my scripting language instead?

The one thing that I liked about IronRuby was I could make small changes while the app was running and then re-run the scripts. Any way to do this in C#? Or will have to constantly restart the app?

+1  A: 

You might be able to update your C# code while the app is running, but it's not going to be simple. This thread discusses attempts to do such a thing:

http://www.eggheadcafe.com/software/aspnet/30778032/assemblyload-and-updat.aspx

At the bottom of that thread is a link to this article, which looks helpful:

http://dotnet.sys-con.com/node/113340

The basic approach is to load your plugin assembly in a separate AppDomain, and then unload the entire AppDomain when you want to replace your plugin.

Nate C-K
+1  A: 

Embedding IronRuby for scripting isn't too hard.
Jimmy Schementi (one of the IronRuby devs) has a complete, detailed example of this here:

http://blog.jimmy.schementi.com/2009/12/ironruby-rubyconf-2009-part-35.html

I don't think this will be very easy to do with C#.

Sorry, I know this doesn't answer your question but hopefully it will be of some use for those trying to deal w/ scripting through IronRuby.

Kevin Radcliffe
I actually love IronRuby. But also wish there was a C# that I could plug-in via the DLR or as easy as the DLR. I have about 20 scripts in this app that just aren't going to make it through or internal standards review. The DLR might be able to pass if Microsoft kept of with the JavaScript on the DLR. My company has approved of JavaScript and most of our devs already know it.
tyndall
@tyndall have you looked at the mono project? They have already have the compiler as a service which allows you to do very similar stuff but with C# and without the DLR
Casual Jim
+3  A: 

At the moment you can't use C# as a scripting language, unless you switch to Mono.

Microsoft have stated that this (or similar functionality) is on the roadmap for C# version 5, which is far into the future.

You can currently fake it however, by creating a temporary "code file" as an in memory string, externally running the C# compiler to produce a new in-memory assembly, and then loading and executing that assembly.
This will work well once, but if you want to update it without restarting you'll have to load the in-memory assembly in a new appdomain, and unload the old one each time (which gets quite tricky).

To be honest, I wouldn't bother. C# doesn't make for a very good scripting language due to it's compiled nature and static typing

Orion Edwards
+1  A: 

You could always try using CS Script.

http://www.csscript.net/

Personally, though, I'd suggest sticking to either IronRuby or Lua for scripting.

FauxBestaan
I have looked at that? how active / stable is the development on the Lua for .NET projects? I never tried the implementation.
tyndall
It seems like a good enough solution for most, but I recently switched from it in one of my projects to hosted IronRuby, as my scripts needed more power without the hassle of registering too many things with Lua. If you're interested in using Lua with .Net, there's a really nice custom attribute for Lua commands, written by Martin Echenique. It can be found here: http://www.gamedev.net/reference/articles/article2275.aspThis will prevent you from needing to manually register any methods that are to be available to your Lua scripts.
FauxBestaan