IronRuby,DLR,范围包含

时间:2010-08-27 08:35:36

标签: c# ironruby dynamic-language-runtime

我想要做的是将正在运行的应用程序的AppDomain引入加载的(Iron)Ruby脚本。

这是我想要实现的一个例子:


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

代码不起作用,因为在IronRuby引擎中没有加载AppDomain或范围(我不确定实际上是什么)。

所以问题是,我如何使代码工作(打印“Hello World!”)?

1 个答案:

答案 0 :(得分:0)

在这里,我们可以使用运行时加载程序集:


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