如何使用PythonNET和C#.NET将枚举放入列表中? [英] How to put an Enum into a List, using PythonNET and C#.NET?

查看:70
本文介绍了如何使用PythonNET和C#.NET将枚举放入列表中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个正在使用PythonNET和NET的NET库,无法弄清楚如何将枚举放入列表.似乎Python将枚举转换为不适合List数据类型的整数.这是一个示例:

I have a NET library I'm using from Python with PythonNET and can't figure out how to put an enum into a List. It seems Python converts the enum to an integer that won't fit the List datatype. Here's an example:

import clr
from System.Collections.Generic import List
from System import Array, Enum
import MyLibrary

enum = List[MyLibrary.ResultType] #no errors here
enum.Add(MyLibrary.ResultType.PV) 
#TypeError: No method matches given arguments

#and just typing MyLibrary.ResultType.PV I get this result
Out[7]: 0

所以我也尝试创建一个数组-它还将创建一个带有枚举数据类型的空数组,但不允许我为其分配值:

So I tried also creating an array - it will also create an empty one with the enum datatype, but won't allow me to assign a value to it:

Array[MyLibrary.ResultType](MyLibrary.ResultType.PV) 
#TypeError: Cannot convert 0 to MyLibrary.ResultType[]

有人对此问题有解决方案吗?欣赏它.

Anyone have a solution to this issue? Appreciate it.

推荐答案

噢,天哪,我不敢相信自己犯的错误,但事实确实如此.我没有通过初始化创建列表

Oh my god I can't believe the mistake I made, but here it is. I wasn't creating the List with an initialization

enum = List[MyLibrary.ResultType]() #the missing () was the reason this didn't work!
enum.Add(MyLibrary.ResultType.PV)

现在它可以正常工作了.

Now it works just fine.

这篇关于如何使用PythonNET和C#.NET将枚举放入列表中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆