Pass an array from IronRuby to C #

I am sure this is a simple fix, and I just can't find it, but here goes:

I have a C # class (let it be called Test) in the assembly (say, SOTest.dll). Here is something similar to what I am doing:

private List<string> items; public List<string> list_items() { return this.items; } public void set_items(List<string> new_items) { this.items = new_items; } 

In the IronRuby interpreter, I run:

 >>> require "SOTest.dll" true >>> include TestNamespace Object >>> myClass = Test.new TestNamespace.Test >>> myClass.list_items() ['Apples', 'Oranges', 'Pears'] >>> myClass.set_items ['Peaches', 'Plums'] TypeError: can't convert Array into System::Collections::Generic::List(string) 

I get a similar error if I can make the argument a 'List <string>', 'List <object>' or 'string []'.

What is the correct syntax? I can't find documented type mapping anywhere (because it is probably too hard to define in certain scenarios given what Ruby can do).

EDIT:

It doesn't seem like I'm trying to make this possible. I need to include the IronRuby build in a .NET project, so the input can be of type IronRuby to clear the scripting interface.

If someone comes up with a way to make it work the way I originally wanted, I will modify the accepted answer.

+4
source share
2 answers

You will have to build the list a little differently:

 ls = System::Collections::Generic::List.of(String).new ls.add("Peaches") ls.add "Pulms" 
+3
source

Never used it, but I am assuming something like:

 myClass.set_items(System::Collections::Generic::List(string).new ['Peaches', 'Plums']) 

That is, build a List<string> from an array. I doubt the System::Collections::Generic::List(string) , but judging by the error message, we give the full name List<string> in IronRuby.

+1
source

Source: https://habr.com/ru/post/1304623/


All Articles