How to put an Enum into a List, using PythonNET and C#.NET?

前提是你 提交于 2021-02-08 02:35:23

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/44078042/how-to-put-an-enum-into-a-list-using-pythonnet-and-c-net

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!