2

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!

2
  • Possible duplicate of Initialize C# List<T> from IronPython? Commented Oct 19, 2015 at 13:39
  • 1
    @FrankV it' s complet different. I want to create my OWN generic class in IronPython, not just use tham. Commented Oct 19, 2015 at 13:42

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

1 Comment

Ok, works perfectly. But when i want to add some custom methods to B, why must the declared in _B. If i declare them in B, i get the message: _B as no attribute Add2? Placing Add2 in _B and every thing works. Thank you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.