Adding scripting functionality to .NET applications

21,056

Solution 1

Oleg Shilo's C# Script solution (at The Code Project) really is a great introduction to providing script abilities in your application.

A different approach would be to consider a language that is specifically built for scripting, such as IronRuby, IronPython, or Lua.

IronPython and IronRuby are both available today.

For a guide to embedding IronPython read How to embed IronPython script support in your existing app in 10 easy steps.

Lua is a scripting language commonly used in games. There is a Lua compiler for .NET, available from CodePlex -- http://www.codeplex.com/Nua

That codebase is a great read if you want to learn about building a compiler in .NET.

A different angle altogether is to try PowerShell. There are numerous examples of embedding PowerShell into an application -- here's a thorough project on the topic: Powershell Tunnel

Solution 2

You might be able to use IronRuby for that.

Otherwise I'd suggest you have a directory where you place precompiled assemblies. Then you could have a reference in the DB to the assembly and class, and use reflection to load the proper assemblies at runtime.

If you really want to compile at run-time you could use the CodeDOM, then you could use reflection to load the dynamic assembly. Microsoft documentation article which might help.

Solution 3

If you don't want to use the DLR you can use Boo (which has an interpreter) or you could consider the Script.NET (S#) project on CodePlex. With the Boo solution you can choose between compiled scripts or using the interpreter, and Boo makes a nice scripting language, has a flexible syntax and an extensible language via its open compiler architecture. Script.NET looks nice too, though, and you could easily extend that language as well as its an open source project and uses a very friendly Compiler Generator (Irony.net).

Solution 4

You could use any of the DLR languages, which provide a way to really easily host your own scripting platform. However, you don't have to use a scripting language for this. You could use C# and compile it with the C# code provider. As long as you load it in its own AppDomain, you can load and unload it to your heart's content.

Solution 5

I'd suggest using LuaInterface as it has fully implemented Lua where it appears that Nua is not complete and likely does not implement some very useful functionality (coroutines, etc).

If you want to use some of the outside prepacked Lua modules, I'd suggest using something along the lines of 1.5.x as opposed to the 2.x series that builds fully managed code and cannot expose the necessary C API.

Share:
21,056

Related videos on Youtube

Michael Stum
Author by

Michael Stum

The same thing we do every night, Pinky. Try to take over the world! Full-Stack Developer on Stack Overflow Enterprise, working to make our little corner of the Internet better for all of us.

Updated on July 05, 2022

Comments

  • Michael Stum
    Michael Stum almost 2 years

    I have a little game written in C#. It uses a database as back-end. It's a trading card game, and I wanted to implement the function of the cards as a script.

    What I mean is that I essentially have an interface, ICard, which a card class implements (public class Card056: ICard) and which contains a function that is called by the game.

    Now, to make the thing maintainable/moddable, I would like to have the class for each card as source code in the database and essentially compile it on first use. So when I have to add/change a card, I'll just add it to the database and tell my application to refresh, without needing any assembly deployment (especially since we would be talking about 1 assembly per card which means hundreds of assemblies).

    Is that possible? Register a class from a source file and then instantiate it, etc.

    ICard Cards[current] = new MyGame.CardLibrary.Card056();
    Cards[current].OnEnterPlay(ref currentGameState);
    

    The language is C# but extra bonus if it's possible to write the script in any .NET language.

    • mattytommo
      mattytommo about 12 years
      That's funny, me and a friend were thinking of writing a trading card game in C# a while back, don't suppose you still have the source for this? Interested on how you'd approached this.
    • Michael Stum
      Michael Stum about 12 years
      @mattytommo No, don't have anything left, it was in the really early stages and essentially was just working like I outlined above. Nowadays, I would look into Roslyn to do C# compilation: blogs.msdn.com/b/csharpfaq/archive/2011/10/19/… - Alternatively, JavaScript using Jint - jint.codeplex.com
    • mattytommo
      mattytommo about 12 years
      ah thanks, but I was looking more for the implementation of the trading card game itself and the structure you'd used, as opposed to the scripting engine. Thanks anyway :)
  • RCIX
    RCIX over 14 years
    LuaInterface is a lua interpreter that works fantastic as well.
  • David Robbins
    David Robbins over 14 years
    I implemented C# Script in a workflow system in Nov 09. It has performed really well for us.
  • Admin
    Admin over 13 years
    (No need to flag this; while this should be a comment/answer update, but is grandfathered in from before those were options)
  • Eric Falsken
    Eric Falsken almost 11 years
    Yes. Although Roslyn is still on the horizon, Mono.CSharp (available on NuGet) packs all the same functionality.
  • Riv
    Riv about 7 years
    Adding an update to keep the options relevant: The .NET Compiler Platform ("Roslyn") is available. So its a viable alternative to all the others mentioned. github.com/dotnet/roslyn.