c# - 如何将 C# 类型转换为 IronRuby 类型,反之亦然?

标签 c# ironruby

例如:

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_stringto_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/

相关文章:

c# - 如何处理类型未知且无关紧要的通用字典?

c# - 你如何屏幕抓取?

silverlight - Silverlight 4 应用程序中的 DLR 脚本

c# - 如何将 ruby​​ gem 嵌入到 C# 项目中并从嵌入式 IronRuby 脚本中获取它?

c# - IronRuby 作为 .NET 中的脚本语言

c# - 这是从 C# 使用 PowerShell 打印 'Hello World' 的最简单方法吗?

c# - 如何在没有 XAML 的情况下将换行符插入 TextBlock?

c# - 使用 iTextSharp 将 pdf 显示到网页?

c# - 通过数据键名获取 Gridview 行

c# - 从 IronRuby 返回一个 CLR 类型