带有 IronPython 类型参数的 C# 泛型

发布于 2024-09-24 10:33:59 字数 532 浏览 1 评论 0原文

因此,情况是我有一个名为 Foo 的 C# 泛型类,其模板参数 T 具有 new() 约束。我已经声明了我的类,如下所示:

class Baz
{
    public Baz() { }
}

class Foo<T>
    where T : Baz, new()
{
    // blah blah
}

在 Python 中:

class Bar(Baz):
    def __init__(self):
        """ do various things here """

但是,如果在 Python 中,我尝试执行 Foo[Bar],我会收到一条错误消息,告诉我我的 Bar< /code> 类违反了 Foo 上的约束(即 new() 约束)。

什么给?

So, the situation is I have a C# generic class named Foo with a template parameter T which has the new() constraint. I've declared my classes something like this:

class Baz
{
    public Baz() { }
}

class Foo<T>
    where T : Baz, new()
{
    // blah blah
}

And in Python:

class Bar(Baz):
    def __init__(self):
        """ do various things here """

However, if, in Python, I try to do Foo[Bar], I get an error telling me that my Bar class violates the constraints (namely the new() constraint) on Foo<T>.

What gives?

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

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

发布评论

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

评论(1

瞄了个咪的 2024-10-01 10:33:59

IronPython 对象没有默认构造函数。它们需要携带一些额外的可变状态,即 Python 类型,必须在实例化类时提供。该类型用于在动态调用时解析任何虚拟重载和其他方法。

There's no default constructor for IronPython objects. They need to carry some additional mutable state with them, the Python type, which must be provided when the class is instantiated. That type is used to resolve any virtual overloads and other methods when called dynamically.

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