例如:
ruby 代码(仅供测试):
def process_initial_array (ar)
ar.join(" ")
end
C# 代码: 在这里,我创建了字符串列表并将其传递给 IronRuby
List<string> source_values = new List<string>();
填满;
label2.Text=IronRuby.CSharp.BasicInteraction.calculator(source_values);
namespace IronRuby.CSharp
{
public class BasicInteraction
{internal static string calculator(List<string> source_values)
{
var rubyEngine = Ruby.CreateEngine();
var scope = rubyEngine.ExecuteFile("math_logic.rb");
string result = rubyEngine.Operations.InvokeMember(scope, "process_initial_array", source_values);
return result;
}
}
}
它唤起了:
An unhandled exception of type 'Microsoft.CSharp.RuntimeBinder.RuntimeBinderException' occurred in Anonymously Hosted DynamicMethods Assembly
Additional information: Unable to convert implicitly "IronRuby.Builtins.MutableString" to "string". There is explicit conversion.
好的,我在相关问题中找到了 IronRuby 字符串方法 to_clr_string,所以问题是我在哪里可以找到关于其他类型的相同方法的文档?
最佳答案
在简要了解 IronRuby 之后 source code我只能找到似乎与您的问题相关的 to_clr_string
和 to_clr_type
(转换为 System.Type
)。因此,我假设那些是您需要在内置 ruby 类型和 CLR 类型之间进行转换的唯一转换方法。其他类型应该与其对应的 CLR 类型相同。
请注意,还有其他方法可以将 ruby strings 转换为其他基本类型,例如 int
(to_i
) 和 双
(to_f
).
在您的示例中,您可以将结果显式转换为 string
:
var result = (string)rubyEngine.Operations.InvokeMember(
scope, "process_initial_array", source_values);
这样,您就不必在 Ruby 端调用 to_clr_string
。
关于c# - 如何将 C# 类型转换为 IronRuby 类型,反之亦然?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10767341/