tags:

views:

24

answers:

1

Hello, what I want to do is to introduce the AppDomain of the running Application into the loaded (Iron)Ruby script.

Here is an example of what I want to achieve:


using System;
using Microsoft.Scripting;
using Microsoft.Scripting.Hosting;
using IronRuby;

namespace Testing
{
    public class MainClass
    {
        public MainClass() { }
        public override string ToString() { return "Hello World"; }
        public static void Main()
        {
            ScriptEngine engine = IronRuby.Ruby.CreateEngine();
            ScriptScope scope = engine.CreateScope();
            String code = "p Testing::MainClass.new.to_str";
            ScriptSource script = engine.CreateScriptSourceFromString(code, SourceCodeKind.SingleStatement);
            script.Compile();
            script.Execute(scope);
        }
    }
}

The code doesn't work ofcourse, because the AppDomain, or the scope (I'm not sure what actually) is not loaded in the IronRuby engine.

So the question is, how do I make the code work(print "Hello World!")?

A: 

Here we go, we can load with the runtime the assemblies:


using System;
using System.Reflection;
using Microsoft.Scripting;
using Microsoft.Scripting.Hosting;
using IronRuby;

namespace Testing
{
    public class MainClass
    {
        public MainClass() { }
        public override string ToString() { return "Hello World"; }
        public static void Main()
        {
            ScriptEngine engine = IronRuby.Ruby.CreateEngine();
            engine.Runtime.LoadAssembly(Assembly.LoadFile(Assembly.GetExecutingAssembly().Location));
            ScriptScope scope = engine.CreateScope();
            String code = "p Testing::MainClass.new";
            ScriptSource script = engine.CreateScriptSourceFromString(code, SourceCodeKind.SingleStatement);
            script.Execute(scope);
        }
    }
}
Andrius Bentkus