C# Evaluator Thursday, November 06, 2008 Pablo Santos dotnet , mono 5 Comments



$ 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





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

$ make

$ make install





$ export MONO_PATH=/home/pablo/monobin

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





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);

}

}

}





$ gmcs test.cs -r:gmcs.exe



Pablo Santos I'm the CTO and Founder at Códice.

I've been leading Plastic SCM since 2005. My passion is helping teams work better through version control.

I had the opportunity to see teams from many different industries at work while I helped them improving their version control practices.

I really enjoy teaching (I've been a University professor for 6+ years) and sharing my experience in talks and articles.

And I love simple code. You can reach me at I'm the CTO and Founder at Códice.I've been leading Plastic SCM since 2005. My passion is helping teams work better through version control.I had the opportunity to see teams from many different industries at work while I helped them improving their version control practices.I really enjoy teaching (I've been a University professor for 6+ years) and sharing my experience in talks and articles.And I love simple code. You can reach me at @psluaces

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.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 reposAnd launch the build processNote 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: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.How to build the code?Easy:And you're done.Remember I've modified both PATH (to get the new gmcs) and MONO_PATH!