How do you get the name of a generic class using reflection?
How do you get the name of a generic class using reflection
eg
public class SomeGenericClass<T>
{
}
SomeGenericClass<int> test = new SomeG开发者_运维百科enericClass<int>();
test.GetType().Name
returns "SomeGenericClass'1"
How do I get it to return "SomeGenericClass" without the '1?
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];
enum.GetName(test.GetType(), test).ToString()
How about just the following?
test.GetType().Name.Split('\'')[0]
It works on non-generic classes too.
精彩评论