【问题标题】:How do I embed iron ruby into a c# program?如何将铁红宝石嵌入到 C# 程序中?
【发布时间】:2011-03-20 21:15:26
【问题描述】:

我想将红宝石嵌入我正在制作的泥浆中,但由于某种原因,我无法找到正确的示例来开始使用。

我想要做的就是创建一个游戏,其中“你”玩家将使用 Iron Ruby 编写机器人程序,然后我将解释 C# 中的代码并让机器人按照你的意愿去做。另外,我想这样做,以便可以将代码解析为铁红宝石代码的字符串,然后我将使用它来控制机器人。

我了解 dlr 和 clr 是两个不同的东西但找不到样本的事实。实际游戏是一个经典的 telnet 服务器,我从头开始用 C# 编写代码,并通过严格的 telnet 协议进行连接。

可以在此处找到:pttmud.the-simmons.net : 4243 通过 telnet 客户端,它应该可以工作。

【问题讨论】:

    标签: c# clr embed ironruby dynamic-language-runtime


    【解决方案1】:

    不完全是 Ruby,下面是我对 IronPython 所做的(确保首先引用程序集):

            runtime = IronPython.Hosting.Python.CreateRuntime();
        }
    
        public void Run(string script, params object[] variables)
        {
            var scriptSource = runtime.GetEngineByFileExtension("py").CreateScriptSourceFromString(script, SourceCodeKind.Statements);
            var scope = runtime.GetEngineByFileExtension("py").CreateScope();
    
            foreach (var variable in variables)
                foreach (var property in variable.GetType().GetProperties())
                    if (property.CanRead)
                        scope.SetVariable(property.Name, property.GetValue(variable, null));
    
            scriptSource.Execute(scope);
    

    这里最重要的元素是脚本引擎。您可以通过不同的选项(在我的情况下为扩展)请求它,并从内存或磁盘源中创建脚本。

    也可以共享变量,这也显示了。

    我认为现在在 C# 4.0 中有更好的方法,因为 C# 本身已成为一种动态编程语言。

    【讨论】:

      【解决方案2】:

      看看这个帖子:http://www.ironshay.com/post/make-your-application-extendable-using-the-dlr.aspx。它展示了如何从 C# 调用 IronRuby/IronPython 代码。

      【讨论】:

        【解决方案3】:

        这是一个如何从 C# 运行 RSpec 的完整示例。

        http://gist.github.com/465677

        但黄瓜不起作用。

        【讨论】:

          猜你喜欢
          • 2010-09-09
          • 1970-01-01
          • 1970-01-01
          • 2010-12-18
          • 2011-07-13
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多