Forum Discussion
ahinterl
Feb 11, 2025Brass Contributor
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.TypeInfoTypeInfo 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
2 Replies
- ahinterlBrass Contributor
($a.GetType()).GetType()Returns:
IsPublic IsSerial Name BaseType -------- -------- ---- -------- False False RuntimeType System.Reflection.TypeInfoTypeInfo 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 - ahinterlBrass Contributor
Found a property that gives me the required information:
$a.GetType().GenericTypeArgumentsoutputs:
IsPublic IsSerial Name BaseType -------- -------- ---- -------- True True String System.ObjectThat's all I need to know 🙂.
Maybe someone knows of other methods...