4

I'm sure this is an easy fix and I just can't find it, but here goes:

I have a C# class (let's call it Test) in an assembly (let's say SOTest.dll). Here is something along the lines of what I'm 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 whether I make the argument a 'List< string >', 'List< object >' or 'string[ ]'.

What is the proper syntax? I can't find a documented mapping of types anywhere (because it's likely too complicated to define in certain scenarios given what Ruby can do).

EDIT:

It doesn't look like what I was trying to do is possible. I'll have to include the IronRuby assembly in the .NET project so the input can be an IronRuby type to keep the scripting interface cleaner.

If anybody comes up with a way to make it work how I originally wanted, I'll change the accepted answer.

2 Answers 2

3

You will have to construct the list a bit differently:

ls = System::Collections::Generic::List.of(String).new
ls.add("Peaches")
ls.add "Pulms"
Sign up to request clarification or add additional context in comments.

2 Comments

Is that really true? The List<string> has a constructor that takes an IEnumerable<string>, which surely an IronRuby array must support.
And how do I create an array of strings?
1

Never used it, but I'm guessing something like:

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

That is, construct a List<string> from the array. I'm doubtful of the System::Collections::Generic::List(string) part, but judging from the error message, that's how to give the fully qualified name of a List<string> in IronRuby.

2 Comments

Surprisingly, this doesn't work (TypeError: can't convert Array into Fixnum). I think that RubyArray inherits from IEnumerable, so I thought it would work.
Sounds like it's trying to call the wrong overload of the List<T> constructor. Maybe that isn't working right in IronRuby yet.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.