Click here to Skip to main content
15,881,898 members
Articles / Programming Languages / C#
Article

.NET scripting, a new approach

Rate me:
Please Sign up or sign in to vote.
3.22/5 (10 votes)
15 Nov 20043 min read 45.7K   422   25   7
A new(?) approach to scripting in .NET applications.

Sample Image - scripting.png

Introduction

This project is an example of how to provide scripting support in your application without having to use a scripting host or create a new application domain.

Background

I've come across many articles on how to use C# as a scripting language. All have focused on either shell or runtime scripting. By runtime scripting, I mean scripts that can be modified at runtime with immediate effects in the application. My approach is slightly different. I compile scripts at runtime, but only once. If the user wants to change a script then the application has to be restarted for the changes to have effect. This is a limitation, but I don't feel that it is a serious one. The benefits are the following:

  • No overhead for passing objects over application boundaries
  • No need to specify interfaces between the script and your application
  • Familiar OO-model for scripting

This scripting solution simply allows the user to extend the functionality of any class in your application. All code that is written in the script files will be treated as if it was part of the original code.

Using the code

Presuming that you have a class named ScriptedClass, it's pretty dumb and doesn't utilize all info we are feeding it.

C#
public class ScriptedClass
{
    private string name;
    public string Name
    {
        get
        {
            return name;
        }
    }

    private int age;
    public int Age
    {
        get
        {
            return age;
        }
    }

    public ScriptedClass()
    {
    }

    public ScriptedClass(string name, int age)
    {
        this.name=name;
        this.age=age;
    }

    public virtual string Message
    {
        get
        {
            return string.Format("Hi {0}!", name);
        }
    }
}

To use the ScripterEngine, you first have to initiate it. The Init-method takes three arguments, one path to the directory that contains the script files, one path to the directory to use for temporary files, and a delegate used for progress:

C#
ScripterEngine.Init(
    string.Format("{0}/script", Application.StartupPath),
    string.Format("{0}/tmp", Application.StartupPath),null);

Then you can create an instance of your class using either any of the constructors.

C#
//Default constructor
ScriptedClass clsa=
  (ScriptedClass)ScripterEngine.CreateObject(typeof(MyClass),
  Type.EmptyTypes,null);

//Default constructor, shorter form
ScriptedClass clsb =
  (ScriptedClass)ScripterEngine.CreateObject(typeof(MyClass));

//Constructor with arguments
ScriptedClass clsc =
  (ScriptedClass)ScripterEngine.CreateObject(typeof(MyClass),
  new Type[]{typeof(string),typeof(int)},
  new object[]{"Hugo",23});

If you, or a user, would like to extend the functionality of this class, you have to write a script.

First, we need a configuration file for the script. This file must be placed in the script directory together with the source file.

XML
<?xml version="1.0" encoding="utf-8" ?>
<ScriptConfiguration name="Intelligent messenger" load="true">
    <References>
        <Reference>System.Web</Reference>
    </References>
    <Files>
        <File>messenger.cs</File>
    </Files>
    <Types>
        <Type>ScriptingArchitecture.ScriptedClass,
                    ScriptingArchitecture</Type>
    </Types>
</ScriptConfiguration>

As you can see, it's plain XML. The References tag contains names of assemblies that are necessary to compile the script. We don't really need the System.Web assembly, but I put it there to illustrate the syntax. All loaded assemblies are automatically used as references when compiling the script, so there isn't any need to add references to any basic assemblies.

The Files tag contains all the source files that compose the script. There can be one or more files in a script.

The Types tag contains all the types that are extended by the script. Note that the type must be specified using the full name of the type followed by the name of the assembly the type is defined in.

When we get this file, we can create the file for the actual script.

C#
using ScriptingArchitecture;
using System.Windows.Forms;
using System.Drawing;

namespace Messenger
{
    public class IntelligentMessage : ScriptingArchitecture.ScriptedClass
    {
        public IntelligentMessage():base()
        {
        }

        public IntelligentMessage(string name, int age):base(name,age)
        {
        }

        public override string Message
        {
            get
            {
                return string.Format("Hi {0}! You are {1} years old!", Name, Age);
            }
        }
    }
}

Points of Interest

To make it possible to accommodate ad-hoc scripts form different sources, the scripting engine uses inheritance chains. If a bunch of scripts extends a particular class in your application, they must all co-exist. So, in this scenario where your class is A:

B:A
C:A
D:A
E:A

The code is rewritten to:

B:A
C:B
D:C
E:D

And the class E is supplied when a class of the type A is requested.

What I miss in my scripting implementation is a more sugary syntax for creating scripted objects.

C#
ScriptedClass clsc=(ScriptedClass)ScripterEngine.CreateObject(
    typeof(MyClass), 
    new Type[]{typeof(string),typeof(int)}, 
    new object[]{"Hugo",23});

Isn't exactly beautiful code. I would like something more along the lines with:

C#
ScriptedClass clsc=new ScriptedClass("Hugo",23);

This can be fixed with a static method in ScriptedClass, but I would like a fix to this that is less of a hack.

C#
public static ScriptedClass GetInstance(string name, int age)
{
    return (ScriptedClass)ScripterEngine.CreateObject(
        typeof(MyClass),
        new Type[]{typeof(string),typeof(int)},
        new object[]{name,age});
}

Caveats

There are some limitations to script code that does not exist in normal development:

  • You are limited to one namespace per source file
  • The class that your script extends must be referenced with its full name, i.e.,
    C#
    public class Script : MyApplication.ScriptedObject

    not

    C#
    public class Script : ScriptedObject
  • You must implement all constructors that are implemented in the base class.

History

  • 2004-11-15

    Uploaded the first version.

  • 2004-11-16

    Source file updated and caveats list added.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
Sweden Sweden
I'm 23 years old and I study informatics at the university of Lund. I'm currently working on my bachelor thesis on predicate logic in .Net.

When it comes to development I'm totally hooked on .Net, it is probably the most exiting platform to work on today, and I'm looking forward to its second incarnation in Longhorn.

Comments and Discussions

 
QuestionPermission to use this in my own project? Pin
Colin Josey21-Mar-07 13:59
Colin Josey21-Mar-07 13:59 
AnswerRe: Permission to use this in my own project? Pin
Hugo Wetterberg22-Mar-07 3:53
Hugo Wetterberg22-Mar-07 3:53 
QuestionFactors Pin
steenhother16-Nov-05 9:58
steenhother16-Nov-05 9:58 
GeneralRegex .NET2 beta 2 issue Pin
Anonymous24-Jun-05 8:11
Anonymous24-Jun-05 8:11 
GeneralSecurity Pin
Dave Bacher15-Nov-04 9:02
Dave Bacher15-Nov-04 9:02 
GeneralRe: Security Pin
Hugo Wetterberg15-Nov-04 21:29
Hugo Wetterberg15-Nov-04 21:29 
GeneralRe: Security Pin
Rei Miyasaka25-Nov-04 11:18
Rei Miyasaka25-Nov-04 11:18 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.