python - 如何使用 CPython 让泛型在 Python.NET 中工作

标签 python .net clr ironpython python.net

如何使用 CPython 让泛型在 Python.NET 中工作。使用 Python.NET Using Generics 中的下标语法时出现错误

TypeError: unsubscriptable object

使用Python 2.7.11 + pythonnet==2.1.0.dev1

>python.exe
Python 2.7.11 (v2.7.11:6d1b6a68f775, Dec  5 2015, 20:32:19) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import clr
>>> from System import EventHandler
>>> from System import EventArgs
>>> EventHandler[EventArgs]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unsubscriptable object

我还尝试过 pythonnet==2.0.0 并使用 ReleaseWin x86 构建 github ca15fe8 并得到了相同的错误。

使用 IronPython-2.7.5:

>ipy.exe
IronPython 2.7.5 (2.7.5.0) on .NET 4.0.30319.0 (32-bit)
Type "help", "copyright", "credits" or "license" for more information.
>>> import clr
>>> from System import EventHandler
>>> from System import EventArgs
>>> EventHandler[EventArgs]
<type 'EventHandler[EventArgs]'>

最佳答案

您可以显式获取通用类对象:

EventHandler = getattr(System, 'EventHandler`1')

数字表示通用参数的数量。

关于python - 如何使用 CPython 让泛型在 Python.NET 中工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35494331/

相关文章:

c# - WIX 可以修改 XAP 包内的 XML 文件吗?

python - 将 SDF 添加到管道,而无需传递虚拟输入

python - 动态配置解析器(Python)

c# - 为什么 Assert.AreEqual() 在比较之前转换为对象?

c# - OrderBy、ObservableCollection<动态>、ICompare

c# - 防止 Thread.CurrentPrincipal 跨应用程序域传播

python - pymacs:一般问题和安装问题

javascript - 如何在前端JavaScript中访问Python(Flask)传递的数组(JSON)?

c# - 从 C++ 调用 C# dll

c# - 为什么 .NET JIT 编译器决定不内联或优化对没有副作用的空静态方法的调用?