Skip to content

Generic class nested within a class cannot be instantiated #1906

Open
@hasslerb

Description

@hasslerb

Environment

  • Pythonnet version: 2.5.2
  • Python version: 3.7.9
  • Operating System: Windows 11
  • .NET Runtime: 4.8.04161

Details

When I try to instantiate a C#-defined generic class in python, I see an error if the generic class is nested within another class.

Consider 3 types of classes

namespace Bar
{
    public class GenericClass<T>
    {
        public T Dummy;
    }

    public static class FooStatic
    {
        public class GenericClassNestedStatic<T>
        {
            public T Dummy;
        }
    }

    public class Foo
    {
        public class GenericClassNested<T>
        {
            public T Dummy;
        }
    }
}

If I run this in python, it executes without issues

from System import UInt32

a = getattr(Bar, "GenericClass`1")
b = a[UInt32]()

However, when I try

foo = Bar.Foo()
c = getattr(foo, "GenericClassNested`1")
d = c[UInt32]()

or

c = getattr(Bar.FooStatic, "GenericClassNestedStatic`1")
d = c[UInt32]()

they both result in

d = c[UInt32]()
TypeError: no type matches params

Is this capability simply not supported, or am I approaching this incorrectly?

Additionally, if I rename my classes the same with a differently scoped context,

namespace Bar
{
    public class GenericClass<T>
    {
        public T Dummy;
    }

    public class Foo
    {
        public class GenericClass<T>
        {
            public T Dummy;
        }
    }
}

when I execute

foo = Bar.Foo()
c = getattr(foo, "GenericClass`1")
d = c[UInt32]()

this succeeds, but it seems to create an instance of Bar.GenericClass rather than Bar.Foo.GenericClass

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions