C# Evaluator

Thursday, November 06, 2008 , 5 Comments

A month ago or so one of the most exciting new feaures of the upcoming Mono 2.2 was introduced: the C# Evaluator. (Although now, with the new Mono.SIMD, static compilation and so on, I'm not sure which one is the most exciting piece anymore?)

Since then I wanted to blog about it to share my thoughts about the possibilities of this new piece of software. For the folks being around for a while C# Evaluator probably reminded them the old Windows Scripting Host which allowed us programmers to embed powerful scripting in our applications. Now, having C# as a scripting language for your own customization purposes opens new paths for innovation and great ideas.

I'm really eager to include some powerful C# scripting inside Plastic , probably in order to create more powerful workspace selector rules or something like that.

But first I'll try to tell a little bit about how to play with the C# eval in order to learn how it works. First thing is to build and set up the latest mono, so let's start there.

Setting up a 2.2 environment

Once Mono 2.0 was releases I set up a OpenSuse 11 box to play around with it. I was having some trouble building 2.0 on Solaris and OpenSolaris, so having the chance to download and build on Linux was a real pleasure.

So, first I installed OpenSuse 11 with the development tools that can be configured during the installation process. Then I set up 2.0 downloading it from the official Mono repository (check the mono website and go to downloads).

And then I just downloaded trunk from the mono repos


$ svn co svn://anonsvn.mono-project.com/source/trunk/mono
$ svn co svn://anonsvn.mono-project.com/source/trunk/mcs
$ svn co svn://anonsvn.mono-project.com/source/trunk/libgdiplus


And launch the build process


$ autogen.sh --prefix=/home/pablo/monobin
$ make
$ make install


Note I generated the binaries on my home dir.

I built everything under /home/pablo/monobin because I still wanted to use official mono 2.0 for regular operations and 2.2 just for testing and development purposes.

In order to use the newly compiled mono 2.2 you just have to set up your MONO_PATH env var:


$ export MONO_PATH=/home/pablo/monobin
$ export PATH=/home/pablo/monobin/bin/:$PATH


Enter the C# Evaluator
Miguel introduced a sample in his blog post (http://tirania.org/blog/archive/2008/Sep-10.html) but he didn't tell how to perform an action I consider very interesting: making the script interact with your own classes. (Of course I bet he didn't do that because it is more than obvious for most of the people out there, but I hope you still find this sample interesting).

So, as I said, the real beauty of introducing some scripting in your application is not running simple calculations, but making the script interact with your own internal classes or ad-hoc APIs, right?

So, here you can find some very simple source code (the simplest) showing you how to interact with your own assembly. Note I'm adding the current assembly to the script references, and also introducing a using sentence.


using Mono.CSharp;
using System.Reflection;
using System;

namespace CSharpEvalTest
{
public class Test
{
public static int val = 1000;

static void Main(string [] args)
{
Evaluator.Run(
"using System; using CSharpEvalTest;");

Evaluator.ReferenceAssembly(
Assembly.GetExecutingAssembly());

Evaluator.Run("Test.val = 120;");
Console.WriteLine("val is {0}", val);
}
}
}

How to build the code?

Easy:


$ gmcs test.cs -r:gmcs.exe


And you're done.

Remember I've modified both PATH (to get the new gmcs) and MONO_PATH!

5 comments:

  1. Nice post, but you should really be pointing people at our recommended instructions for setting up a parallel mono environment:

    http://www.mono-project.com/Parallel_Mono_Environments

    They cover a few more corners.

    ReplyDelete
  2. What autogen are you using? If I follow the official howto there's little info and only a non-working URL. I've installed autogen 5.9.5 through my package manager but when I try to do as you write I get "autogen: illegal option -- prefix=/home/casper/monobin"

    ReplyDelete
  3. Hi Casper,

    It's the autogen.sh which comes with the Mono sources, right? Maybe I typed it wrong, but I don't think so...

    ReplyDelete
  4. Hi Pablo. Yes it is, but autoconf itself doesn't come with mono?! I was just wondering what distro you are on. Because even if I install autoconf in Ubuntu through apt-get, autogen.sh complains with "**Error**: You must have `autoconf' installed to compile Mono."

    I had some trouble building from source in the past, but saw your nice howto and thought I'd give it a try again.

    ReplyDelete
  5. Any ideas for this error?
    {interactive}(1,22): error CS0246: The type or namespace name `CSharpEvalTest' could not be found. Are you missing a using directive or an assembly reference?
    {interactive}(1,2): error CS0103: The name `Test' does not exist in the current context

    ReplyDelete