繁体   English   中英

获取没有名称空间的枚举或类的字符串名称?

[英]Get the string name of an enumeration or class without namespace?

所以,我想得到枚举或类的名称,而不在其前面附加完整的命名空间...例如:

enum MyEnum {
    // enum values here
}

// somewhere else in the code
string testString = ????  // ???? returns "MyEnum"

typeof(MyEnum)主要工作,但枚举的命名空间附加到前面。

任何帮助将不胜感激...谢谢!

使用.Name只获取字符串中的类型,如下所示:

string testString = typeof(MyEnum).Name;

以下是一些例子:

typeof(String).Name // "String"
typeof(String).FullName // "System.String"

像上面的例子一样, .FullName给出了完整的类型名称,包括命名空间。

暂无
暂无

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

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