考虑以下两个类:
class GenericClass<T> where T : class
{
}
class PlainClass
{
}
我希望能够像"GenericClass"
一样获得GenericClass
的名称。对于PlainClass
,我可以使用nameof
做到这一点,但这对泛型类不起作用:
var plainName = nameof(PlainClass);
var gemericName = nameof(GenericClass)//does not compile
var gemericName1 = typeof(GenericClass<>).Name; // "GenericClass`1"
发布于 2019-02-14 12:37:56
不是很直接,但是
nameof(GenericClass<object>)
计算结果为
"GenericClass"
https://stackoverflow.com/questions/54690543
复制