Forum Discussion

ahinterl's avatar
ahinterl
Brass Contributor
Feb 11, 2025
Solved

How to find out the type of a generic class?

Exmple: create a new List<string> instance:

$a = [System.Collections.Generic.List[string]]::new()

With:

$a.GetType()

I only get:

IsPublic IsSerial Name                                     BaseType
-------- -------- ----                                     --------
True     True     List`1                                   System.Object

How can I tell what type the list is (in my case, string)?

  • ($a.GetType()).GetType()

    Returns:

    IsPublic IsSerial Name                                     BaseType
    -------- -------- ----                                     --------
    False    False    RuntimeType                              System.Reflection.TypeInfo

    TypeInfo derives from System.Type, which has this method:

    $a.GetType().GetGenericArguments()[0]

    which returns the same as in my previous post:

    IsPublic IsSerial Name                                     BaseType
    -------- -------- ----                                     --------
    True     True     String                                   System.Object

     

  • ahinterl's avatar
    ahinterl
    Brass Contributor
    ($a.GetType()).GetType()

    Returns:

    IsPublic IsSerial Name                                     BaseType
    -------- -------- ----                                     --------
    False    False    RuntimeType                              System.Reflection.TypeInfo

    TypeInfo derives from System.Type, which has this method:

    $a.GetType().GetGenericArguments()[0]

    which returns the same as in my previous post:

    IsPublic IsSerial Name                                     BaseType
    -------- -------- ----                                     --------
    True     True     String                                   System.Object

     

  • ahinterl's avatar
    ahinterl
    Brass Contributor

    Found a property that gives me the required information:

    $a.GetType().GenericTypeArguments

    outputs:

    IsPublic IsSerial Name                                     BaseType
    -------- -------- ----                                     --------
    True     True     String                                   System.Object

     

    That's all I need to know 🙂.

    Maybe someone knows of other methods...

Resources