简体   繁体   English

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

[英]C# Generics with IronPython Type Parameters

So, the situation is I have a C# generic class named Foo with a template parameter T which has the new() constraint. 所以,情况是我有一个名为Foo的C#泛型类,其模板参数T具有new()约束。 I've declared my classes something like this: 我已经宣布我的课程是这样的:

class Baz
{
    public Baz() { }
}

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

And in Python: 在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> . 但是,如果在Python中我尝试做Foo[Bar] ,我会收到一个错误,告诉我我的Bar类违反了Foo<T>上的约束(即new()约束)。

What gives? 是什么赋予了?

There's no default constructor for IronPython objects. IronPython对象没有默认构造函数。 They need to carry some additional mutable state with them, the Python type, which must be provided when the class is instantiated. 他们需要携带一些额外的可变状态,Python类型,必须在实例化类时提供。 That type is used to resolve any virtual overloads and other methods when called dynamically. 该类型用于在动态调用时解析任何虚拟重载和其他方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM