关于泛型类型的IronPython重载决策
发布时间:2020-10-19 18:03:48 所属栏目:Python 来源:互联网
导读:我有一个带有重载静态方法的C#类,如下所示: // Added to the Simple class in TutorialExtendcsextend.cspublic static int Foo(IEnumerableint values){ return 1;}public static int Foo(IEnumerablestring values){
我有一个带有重载静态方法的C#类,如下所示: // Added to the Simple class in TutorialExtendcsextend.cs public static int Foo(IEnumerable<int> values) { return 1; } public static int Foo(IEnumerable<string> values) { return 2; } 当我尝试从IronPython 2.6中调用它们时出错.我正在传递一个包含字符串的python列表. import clr clr.AddReferenceToFile("csextend.dll") import Simple Simple.Foo(["alpha","bravo","charlie"]) TypeError: Multiple targets could match: Foo(IEnumerable[str]),Foo(IEnumerable[ int]) 我的第一个问题是为什么这不起作用?似乎重载解决方案应该对此起作用.这是IronPython中的错误吗?什么是最干净的解决方法.我可以重命名例程,这样它们就不会相互重载,但后来我让铁心怪怪改变了C#类的设计. 是否有一种干净的方法可以给python一个线索,即列表完全由一种类型组成,它应该选择一个特定的重载? 与this question有关 解决方法IronPython并没有真正的函数重载,只有一个函数具有所有功能.通常IronPython会自动执行此操作,但泛型类型会使事情变得复杂.要消除使用哪个重载的歧义,使用Overloads字典获取函数,将签名中的类型作为键传递. (我在撰写本文时使用的是IronPython 2.7,因此我不知道版本2.6和2.7之间是否存在差异)import System.Collections.Generic.IEnumerable as IEnumerable Simple.Foo.Overloads[IEnumerable[str]](["alpha","charlie"]) 如果函数中有更多参数,则将类型作为元组传递. #public static int Foo(IEnumerable<string> values,string otherParam) #{ # return 3; #} Simple.Foo.Overloads[IEnumerable[str],str](["alpha","charlie"],"x") 等等 (编辑:岳阳站长网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- python – ctypes错误找不到AttributeError符号,OS X 10.7.
- 使用 Python 获取 Linux 系统信息!怪不得企业都要用Linux呢
- 如何为Python 3安装bpython?
- 使用python,自动确定用户当前时区的最准确方法是什么
- python – 用ElementTree写入带有utf-8数据的xml utf-8文件
- 为什么设置比python中的列表大?
- 【python】RuntimeError: Lock objects should only be sha
- python – 替换numpy数组中的元素,避免循环
- 使用NLTK从OCR中识别未分裂的单词
- Django ForeignKey,null = True,内连接和左外连接