我正在尝试运行以下代码:
import clr
import sys
#import System.Collections
clr.AddReference("System.Collections")
from System.Collections.Generic import List
from System import String
class MyClass(object):
def __init__(self, e=""):
self.title = e
li = List[MyClass]()
list(li)
然而,我收到了错误"TypeError:预期类型",无法理解如何解决它。
这与Python 中的System.Collections.Generic列表有关
谢谢!
在Python中,除非执行以下操作之一,否则不能使用.NET列表以强类型方式存储Python对象:
- 从.NET类型派生Python类
class MyClass(System.Object):
# without __namespace__ - regular Python type
__namespace__ = "MyNamespace"
使用
List<System.Object>
或List<Python.Runtime.PyObject>
具有用于.NET类型
X
的带有Python到.NET编解码器的List<X>