IronPython 对泛型类型的重载解析

发布于 2024-09-27 03:39:46 字数 949 浏览 1 评论 0原文

我有一个带有如下重载静态方法的 C# 类:

// Added to the Simple class in Tutorial\Extend\csextend.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 中的错误吗?什么是最干净的解决方法。我可以重命名例程,这样它们就不会互相重载,但随后我让ironpython 的怪癖改变了 C# 类的设计。

有没有一种干净的方法可以让 python 知道列表完全由一种类型组成,并且它应该选择特定的重载?

此问题相关

I have a C# class with overloaded static methods like these:

// Added to the Simple class in Tutorial\Extend\csextend.cs
public static int Foo(IEnumerable<int> values)
{
    return 1;
}

public static int Foo(IEnumerable<string> values)
{
    return 2;
}

I get an error when I try to call these from IronPython 2.6. I am passing a python list that contains strings.

import clr
clr.AddReferenceToFile("csextend.dll")
import Simple

Simple.Foo(["alpha", "bravo", "charlie"])
TypeError: Multiple targets could match: Foo(IEnumerable[str]), Foo(IEnumerable[
int])

My first question is why doesn't this work? It seems like overload resolution should work on this. Is this a bug in IronPython? What is the cleanest workaround. I could rename the routines so they don't overload each other, but then I am letting ironpython quirks alter the design of the C# class.

Is there a clean way to give python a clue that the list is entirely composed of one type, and that it should pick a specific overload?

Related to this question

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

等你爱我 2024-10-04 03:39:46

IronPython 并没有真正的函数重载,只有一个函数具有所有功能。通常,IronPython 会自动执行此操作,但泛型类型使事情变得复杂。要消除使用哪个重载的歧义,请使用 Overloads 字典获取函数,并将签名中的类型作为键传递。 (在撰写本文时,我正在使用 IronPython 2.7,因此我不知道版本 2.6 和 2.7 之间是否存在差异)

import System.Collections.Generic.IEnumerable as IEnumerable
Simple.Foo.Overloads[IEnumerable[str]](["alpha", "bravo", "charlie"])

如果函数中有更多参数,请将类型作为元组传递。

#public static int Foo(IEnumerable<string> values, string otherParam)
#{
#    return 3;
#}

Simple.Foo.Overloads[IEnumerable[str],str](["alpha", "bravo", "charlie"], "x")

ETC。

IronPython doesn't really have overloads of functions, just one single function with all the functionality. Normally IronPython would do this automatically but the generic types complicate things. To disambiguate which overload to use, get the function using the Overloads dictionary passing the types in the signature as the key. (I'm using IronPython 2.7 at the time of writing this so I don't know if there's a difference between version 2.6 and 2.7)

import System.Collections.Generic.IEnumerable as IEnumerable
Simple.Foo.Overloads[IEnumerable[str]](["alpha", "bravo", "charlie"])

If there were more parameters in the functions, pass the types in as a tuple.

#public static int Foo(IEnumerable<string> values, string otherParam)
#{
#    return 3;
#}

Simple.Foo.Overloads[IEnumerable[str],str](["alpha", "bravo", "charlie"], "x")

etc.

尘曦 2024-10-04 03:39:46

我知道这不是OP的确切问题,但这里有一个类似的场景,具有重载和多个参数,以防其他人想知道如何解决它:

public FamilyInstance NewFamilyInstance(
XYZ origin,
FamilySymbol symbol,
View specView
)

public FamilyInstance NewFamilyInstance(
Line line,
FamilySymbol symbol,
View specView
)

以下是如何使用 Line 而不是 XYZ 的特定重载来调用它:

doc.Create.NewFamilyInstance.Overloads[Line, FamilySymbol, View](crv, symbol, view)

这篇文章对我帮助很大。感谢分享!

I know this wasn't the OP's exact question, but here's a similar scenario with overloads and multiple parameters just in case someone else is wondering how to solve it:

public FamilyInstance NewFamilyInstance(
XYZ origin,
FamilySymbol symbol,
View specView
)

public FamilyInstance NewFamilyInstance(
Line line,
FamilySymbol symbol,
View specView
)

And here's how to call it with a specific overload that uses Line instead of XYZ:

doc.Create.NewFamilyInstance.Overloads[Line, FamilySymbol, View](crv, symbol, view)

This post helped me a lot. Thanks for sharing!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文