2

例えば:

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.

OK、関連する質問で IronRuby 文字列メソッド to_clr_string を見つけたので、質問は、他のタイプの同じメソッドに関するドキュメントをどこで見つけることができるかということです?

4

1 に答える 1

1

IronRuby のソース コードを簡単に調べたところ、質問に関連すると思われるto_clr_stringto_clr_type(に変換する)しか見つかりませんでした。System.Typeしたがって、組み込みのruby​​ 型と CLR 型の間で変換する必要がある変換メソッドはこれらだけだと思います。他の型は、対応する CLR と同じ型である必要があります。

ルビ文字列intから( to_i) やdouble( )などの他のプリミティブ型に変換する方法は他にもあることに注意してくださいto_f

あなたの例では、結果を明示的に次のように変換できますstring

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

to_clr_stringこれにより、Ruby 側で呼び出す必要がなくなります。

于 2012-06-03T01:12:06.567 に答える