简体   繁体   中英

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. 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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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