BendEg
BendEg

Reputation: 21128

IronPython create generic class

It is possible to create a generic class in IronPython? I want to inherit from a generic c# class and implement a custom IronPython class on top.

for example:

public class A<T>
{

}

First IronPython class:

class B[T](A[T]): # Problematic part. I don't know how to create a
                  # generic ironpython class

Second IronPython class:

class C(B[object]): 

So in the first IronPython is the problem. I don't now hot to create a generic class to pass the type. Is this possible?

EDIT

I don't just want to use generic c# classes. I want to implement my own in IronPython and inherting from a c# class.

EDIT2

What I want to achive is a class A that has a python base class B, which should have a generic C# baseclass C. The python baseclass B is independent of the type of the C# baseclass C (it's just a specialization of the generic C# class), but it has to initialize the correct C# base class.

Thank you!

Upvotes: 2

Views: 683

Answers (1)

Jeff Mercado
Jeff Mercado

Reputation: 134581

(Iron)Python is a dynamically typed language, there is no such notion of generic classes in python. You can derive from a generic .NET class, but you can't actually create a generic python class, there is no such thing.

You can however mimic the syntax with the use of some decorators.

class B(object):
    class _Meta(type):
        def __getitem__(cls, t):
            from System.Collections.Generic import List as A # just an example
            class _B(A[t]):
                pass
            return _B
    __metaclass__ = _Meta

class C(B[object]):
    pass

Your actual implementation of B is the class _B in the example.

Upvotes: 2

Related Questions