简体   繁体   English

将 Python 列表转换为 .NET IEnumerable?

[英]Converting Python List to .NET IEnumerable?

I'm attempting to call a C# function from a Python script, via the clr module from the PythonNet library.我试图通过PythonNet库中的clr模块从 Python 脚本调用 C# function。

One of the arguments that this C# function takes is of the type System.Collections.Generic.IEnumerable .这个 C# function 采用的 arguments 之一是System.Collections.Generic.IEnumerable类型。 Simply supplying a list of the required data types to the first argument results in a 'list' value cannot be converted to 'System.Collections.Generic.IEnumerable' error.简单地向第一个参数提供所需数据类型的列表会导致'list' value cannot be converted to 'System.Collections.Generic.IEnumerable'错误。

After searching online, the supposed .NET datatypes should already be included in the Python installation, and I should be able to access them.在网上搜索后,假设的 .NET 数据类型应该已经包含在 Python 安装中,我应该可以访问它们。 However, I'm unsure as to how.但是,我不确定如何。

Doing:正在做:

from System.Collections.Generic import *

Fails because the module isn't found, while doing:失败,因为找不到模块,同时执行:

import collections

Doesn't have a namespace IEnumerable .没有命名空间IEnumerable

How would I go about converting a Python list to the System.Collections.Generic.IEnumerable data type?我 go 如何将 Python 列表转换为System.Collections.Generic.IEnumerable数据类型?

Thanks for reading my post, any guidance is appreciated.感谢您阅读我的帖子,感谢任何指导。

Before importing anything from C#/.NET you have to import clr from pytho.net package.在从 C#/.NET 导入任何内容之前,您必须从pytho.net package import clr

After messing around in the Python script for a few hours, I managed to figure out a solution:在Python脚本中折腾了几个小时后,终于想出了解决办法:

First, the actual C# Collections namespace has to be imported via the clr module as such:首先,必须通过clr模块导入实际的 C# Collections命名空间,如下所示:

clr.AddReference("System.Collections")

Then, the specific data type of the Collections namespace has to be imported:然后,必须导入Collections命名空间的特定数据类型:

from System.Collections.Generic import IEnumerable

And then one should be able to use the respective data type, and call all the class functions it has.然后应该能够使用相应的数据类型,并调用它具有的所有 class 函数。 In my case, I wanted to convert a Python List into the IEnumerable datatype.就我而言,我想将 Python List转换为IEnumerable数据类型。 Therefore, I could iterate over my Python List , and call the Add function of the IEnumerable class (using datatype of int as an example):因此,我可以遍历我的 Python List ,并调用IEnumerable class 的Add function(以int数据类型为例):

myList = [1, 2, 3]
myIEnumerable = IEnumerable[int]()

for i in range(len(myList)):
   myIEnumerable.Add(myList[i])

As a sidenote, while the C# function that I wanted to call from my Python script has one of its arguments listed as accepting a type of System.Collections.Generic.IEnumerable , I managed to get it to work with having a System.Collections.Generic.List type instead.作为旁注,虽然我想从我的 Python 脚本调用的 C# function 的其中一个 arguments 被列为接受一种类型System.Collections.Generic.IEnumerable ,但我设法让它与System.Collections.Generic.List一起工作System.Collections.Generic.List类型代替。 Perhaps it'll be useful for someone else.也许它会对其他人有用。

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

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