2010-03-19 49 views
4

数组我敢肯定,这是一个简单的办法,我只是无法找到它,但这里有云:妙传IronRuby的为C#

我有一个C#类(姑且称之为试验)在装配(让我们说SOTest.dll)。 这里是沿着我在做什么东西行:

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) 

我收到了类似的错误我是否做出论证了“列表<串>','列表< object>'或'string []'。

什么是正确的语法?我找不到任何地方类型的文档映射(因为在给定Ruby的某些情况下可能会过于复杂)。

编辑:

它看起来并不像我所试图做的是可能的。我必须在.NET项目中包含IronRuby程序集,以便输入可以是IronRuby类型以保持脚本接口更清晰。

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

回答

3

您必须构建列表有点不同:

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

这是真的吗?该名单''有一个构造函数的IEnumerable的'',这肯定了IronRuby的阵列必须支持。 – 2010-03-21 01:07:12

+0

如何创建一个字符串数组? – Dmitriy 2011-04-02 08:20:24

1

从未使用过它,但我猜是这样的:

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

也就是说,构建从阵列List<string>。我怀疑的System::Collections::Generic::List(string)部分,但是从错误信息来看,这就是如何给在IronRuby的一个List<string>的全名。

+0

出人意料的是,这不起作用(类型错误:不能将数组Fixnum对象)。我认为RubyArray继承自IEnumerable,所以我认为它会起作用。 – cgyDeveloper 2010-03-23 18:11:35

+0

听起来就像它试图调用列表构造函数的错误超载。也许这在IronRuby中还不行。 – 2010-03-23 19:18:47