简体   繁体   English

将数组从IronRuby传递给C#

[英]Pass an array from IronRuby to C#

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). 我在程序集中有一个C#类(让我们称之为Test)(假设是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: 在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)

I get a similar error whether I make the argument a 'List< string >', 'List< object >' or 'string[ ]'. 无论我将参数设为'List <string>','List <object>'还是'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). 我无法在任何地方找到类型的文档映射(因为在某些情况下,由于Ruby可以做什么,它可能太复杂了)。

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. 我将不得不在.NET项目中包含IronRuby程序集,因此输入可以是IronRuby类型,以保持脚本界面更清晰。

If anybody comes up with a way to make it work how I originally wanted, I'll change the accepted answer. 如果有人想出一种方法让它按照我原来想要的方式工作,我会改变接受的答案。

You will have to construct the list a bit differently: 您将不得不以不同的方式构建列表:

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

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. 也就是说,从数组中构造一个List<string> 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. 我怀疑System::Collections::Generic::List(string)部分,但从错误消息判断,这是如何在IronRuby中给出List<string>的完全限定名称。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM