【问题标题】:How to put an Enum into a List, using PythonNET and C#.NET?如何使用 PythonNET 和 C#.NET 将枚举放入列表?
【发布时间】:2017-10-20 01:20:22
【问题描述】:

我有一个 NET 库,我在 Python 中使用 PythonNET,但不知道如何将枚举放入列表中。似乎 Python 将枚举转换为不适合 List 数据类型的整数。这是一个例子:

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

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

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

谁有这个问题的解决方案?欣赏它。

【问题讨论】:

  • 请不要在标题中添加 [已解决的] 设备,Matt - 我们在这里不使用它们。改用接受系统 - 谢谢!
  • 抱歉,我已在 PythonNET 邮件列表上发帖,希望小组在电子邮件中看到它已经解决。已删除。

标签: c# python enums python.net


【解决方案1】:

天哪,我不敢相信我犯的错误,但它就在这里。我没有通过初始化创建列表

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

现在它工作得很好。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-11-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-06
    • 2016-08-03
    相关资源
    最近更新 更多