gpt4 book ai didi

c# - python3 pythonnet 泛型代表

转载 作者:太空宇宙 更新时间:2023-11-03 15:07:50 31 4
gpt4 key购买 nike

我在 windows7 上安装了 64 位 CPython 3.4。我使用 pythonnet 包 (2.0.0.dev1)。我想实例化 Action 委托(delegate),但它给我一个错误。

def display(num):
print("num=", num)

import clr
clr.AddReference("System")
import System

paction=System.Action[System.Int32](display)

我收到这个错误:

TypeError Traceback (most recent call last) in () ----> 1 paction=System.Action[System.Int32](display) TypeError: unsubscriptable object

我想这就是指定泛型的方式。

我检查了文档和this发帖,还是没看出问题。我也尝试了一下 Overload 方法,但也没有帮助:

paction=System.Action.Overloads[System.Int32](display)

TypeError Traceback (most recent call last) in () ----> 1 paction=System.Action.Overloads[System.Int32](display) TypeError: No match found for constructor signature

最佳答案

问题是 System.Action (没有参数,因此不是通用的)正在隐藏 System.Action<T>同时 System.Func直接映射到 System.Func<T> .我想这是因为 System.Func总是有一个泛型参数,并且似乎有一个针对泛型的重载实现。

泛型在 Python.NET 中的名称是 Action`1 (通常:Action`NN 是通用参数的数量)。您可以使用 getattr 获取包装器对象在模块上:

Action = getattr(System, "Action`1")
action = Action[Int32](display)

关于c# - python3 pythonnet 泛型代表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30659933/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com