4

我确定这是一个简单的解决方法,但我找不到它,但这里有:

我在程序集中(比如说 SOTest.dll)中有一个 C# 类(我们称之为 Test)。这是我正在做的事情:

private List<string> items;

public List<string> list_items()
{
    return this.items;
}

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

在 IronRuby 解释器中,我运行:

>>> 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)

无论我将参数设为“List< string >”、“List< object >”还是“string[]”,都会出现类似的错误。

什么是正确的语法?我在任何地方都找不到记录在案的类型映射(因为鉴于 Ruby 可以做什么,在某些情况下定义它可能太复杂了)。

编辑:

看起来我试图做的事情是不可能的。我必须在 .NET 项目中包含 IronRuby 程序集,以便输入可以是 IronRuby 类型,以保持脚本界面更简洁。

如果有人想出一种方法让它按照我最初想要的方式工作,我会改变接受的答案。

4

2 回答 2

3

您将不得不以不同的方式构建列表:

ls = System::Collections::Generic::List.of(String).new
ls.add("Peaches")
ls.add "Pulms"
于 2010-03-20T07:05:17.863 回答
1

从来没有用过,但我猜是这样的:

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

也就是说,List<string>从数组构造 a 。我对此System::Collections::Generic::List(string)部分持怀疑态度,但从错误消息来看,这就是如何List<string>在 IronRuby 中给出 a 的完全限定名称。

于 2010-03-19T23:42:33.713 回答