繁体   English   中英

在IronRuby中无法将X :: Y :: Z转换为X :: Y :: Z

[英]Can't Convert X::Y::Z to X::Y::Z in IronRuby

我在玩IronRuby。 我制作了一个互动小说游戏的存根。 在C#中,我有一个Command类,它表示命令(名称,要键入的文本和操作委托):

公共委托字符串CommandAction(字符串目标,字符串工具,字符串介词); 私有CommandAction动作;

    public Command(string name, string[] verbs, CommandAction action)
    {
        this.Name = name;
        this.Verbs = verbs;
        this.action = action;
    }

我在C#中创建命令没有任何问题。 这是最简单的:

this.knownCommands.Add(new Command("Quit", new string[] { "q", "quit" }, (t, i, p) =>
{
    isRunning = false;
    return "Bye!";
}));

(现在不必担心这三个参数,它们都是字符串。)

我想创建一个命令并将其添加到我的IEnumerable<Command>列表中。 这是Ruby代码:

def to_clr_string_array(list)
    System::Array[System::String].new(list.map { |s| s.to_s.to_clr_string })
end

require 'Meltdown.Core.dll'
include Meltdown::Core

Command.new("Ruby Command", to_clr_string_array(['rb']), Proc.new { |target, preposition, instrument|
    puts "Command invoked with #{target}, #{instrument}, and #{preposition}"
})

to_clr_string_array是转换列表类型所必需的。)尝试实例化此类型时,出现类型转换错误: Can't Convert Meltdown::Core::Command into Meltdown::Core::Command 这是我的方法:

var engine = Ruby.CreateEngine();
var scope = Engine.Runtime.CreateScope();
string contents = System.IO.File.ReadAllText(scriptPath);
var command = engine.Execute<Command>(contents, scope);

第三行失败。 我尝试过解决类似问题的答案 ,并将数组更改为对象列表。 当执行命令时,不是实例化失败,而是得到类型不同的错误。

在该详细错误中,它提到的错误与我链接的答案中的问题完全相同:类型仅在其上下文方面有所不同( DefaultLoadNeither )。

不幸的是,这无济于事,我仍然不知道该怎么做。 我也尝试过将命令列表传递给Ruby,但是我收到一个错误,提示类型不匹配,因此无法进入列表。

我究竟做错了什么?

我将此问题发布到了ironruby-core邮件列表中。 布兰登·杜特(Brandon Doot)回答并建议我补充:

load_assembly 'SharedClasses'

这样就解决了问题。 就.NET而言,类型现在相同。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM