English 中文(简体)
如何将 C # 类型转换为铁鲁比类型, 反之亦然?
原标题:How to convert C# types to IronRuby types and vice versa?
  • 时间:2012-05-26 15:07:54
  •  标签:
  • c#
  • ironruby

例如:

红宝石代码( 仅用于测试) :

def process_initial_array (ar)
     ar.join(" ")
end

c# code: Here I create List of strings and pass it to the 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.

好,我找到铁鲁比字符串方法 在相关问题中找到_clar_string, 所以问题是我在哪里能找到关于 其它类型的相同方法的文件?

问题回答

在简要查看铁路鲁比 < a href > 之后,我只能找到 < code>to_cl_string 和 to_clr_type (转换为 System.Type ),这似乎与你的问题有关。 因此,我`em>assume 这些是你在内建红和CLR类型之间唯一需要转换的转换方法。其他类型 应该和CLR对应类型相同。

请注意,还有从红宝石 strings 转换为其他原始类型的其他方法,如 unt (to_i )和 twon (to_f )。

在您的示例中, 您可以将结果明确转换为 < code> string :

var result = (string)rubyEngine.Operations.InvokeMember(
  scope, "process_initial_array", source_values);

这样, 您不必在 Ruby 一侧调用 < code> to_ clar_ string





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...