本文介绍了如何使用CPython使泛型在Python.NET中工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何使用CPython在Python.NET中使用泛型。使用
How can I get generics to work in Python.NET with CPython. I get an error when using the subscript syntax from Python.NET Using Generics
TypeError: unsubscriptable object
使用Python 2.7.11 + pythonnet == 2.1.0.dev1
With 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,并得到相同的错误。
I've also tried pythonnet==2.0.0 and building form github ca15fe8 with ReleaseWin x86 and got the same error.
使用IronPython-2.7.5:
With 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]'>
推荐答案
您可以显式获得泛型类对象:
You can get the generic class object explicitly:
EventHandler = getattr(System, 'EventHandler`1')
数字表示通用参数的数量。
The number indicates the number of generic arguments.
这篇关于如何使用CPython使泛型在Python.NET中工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!