How can I get generics to work in Python.NET with CPython

做~自己de王妃 提交于 2019-12-06 08:51:54

You can get the generic class object explicitly:

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

The number indicates the number of generic arguments.

That doesn't work because there are both generic and non-generic versions of the EventHandler class that exist in the System namespace. The name is overloaded. You need to indicate that you want the generic version.

I'm not sure how exactly Python.NET handles overloaded classes/functions but it seems like it has an Overloads property. Try something like this and see how that goes:

EventHandler_EventArgs = EventHandler.Overloads[EventArgs]

(this doesn't work)

It doesn't seem like Python.NET has ways to resolve the overloaded class name, each overload is treated as separate distinct types (using their clr names). It doesn't do the same thing as IronPython and lump them into a single containing type.

In this particular case, the EventArgs name corresponds to the non-generic EventArgs type. You'll need to get the appropriate type directly from the System module as filmor illustrates.

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