【发布时间】:2013-09-12 21:34:30
【问题描述】:
我在玩 IronRuby。我制作了一个互动小说游戏的存根。在 C# 中,我有一个 Command 类,它代表一个命令(名称、要键入的文本和操作委托):
public delegate string CommandAction(string target, string instrument, string preposition); 私人 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);
第三行失败。我尝试了this answer to a similar problem,并将我的数组更改为对象列表。当我执行命令时,我得到一个类型不同的错误,而不是实例化失败。
在那个详细的错误中,它提到了与我链接的答案中的问题完全相同的错误:类型仅在其上下文中有所不同(Default 和 LoadNeither)。
不幸的是,这对我没有帮助,我仍然无法弄清楚如何做到这一点。我也尝试将我的命令列表传递给 Ruby,但我收到一个错误,即类型不匹配并且它无法进入我的列表。
我做错了什么?
【问题讨论】: