繁体   English   中英

如何使用反射获取泛型类的名称?

[英]How do you get the name of a generic class using reflection?

如何使用反射获取泛型类的名称

例如

public class SomeGenericClass<T>
{
}

SomeGenericClass<int> test = new SomeGenericClass<int>();

test.GetType().Name返回“ SomeGenericClass'1”

如何获取不带“ 1”的“ SomeGenericClass”?

'1是名称的一部分,因为例如List<T>List (如果我创建了这样的类)是不同的类。

'1表示它具有一个类型参数。 如果您想知道该参数的类型,请使用test.GetType()。GetGenericArguments()[0];。

enum.GetName(test.GetType(), test).ToString()

那么以下呢?

test.GetType().Name.Split('\'')[0]

它也适用于非泛型类。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM