问题
How do you get the name of a generic class using reflection
eg
public class SomeGenericClass<T>
{
}
SomeGenericClass<int> test = new SomeGenericClass<int>();
test.GetType().Name
returns "SomeGenericClass'1"
How do I get it to return "SomeGenericClass" without the '1?
回答1:
How about just the following?
test.GetType().Name.Split('\'')[0]
It works on non-generic classes too.
回答2:
The '1 is part of the name, because, for example, List<T>
and List
(if I created such a class) are different classes.
'1 means that it has one type parameter. If you want to know the type of that parameter, use test.GetType().GetGenericArguments()[0];
回答3:
enum.GetName(test.GetType(), test).ToString()
来源:https://stackoverflow.com/questions/1752823/how-do-you-get-the-name-of-a-generic-class-using-reflection