简体   繁体   English

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

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

So, I'd like to get the name of an enumeration or class without the full namespace appended on to the front of it... For example: 所以,我想得到枚举或类的名称,而不在其前面附加完整的命名空间...例如:

enum MyEnum {
    // enum values here
}

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

typeof(MyEnum) mostly works, however the namespace of the enumeration is appended to the front. typeof(MyEnum)主要工作,但枚举的命名空间附加到前面。

Any help would be appreciated... thanks! 任何帮助将不胜感激...谢谢!

Use .Name to get only the type in the string, like this: 使用.Name只获取字符串中的类型,如下所示:

string testString = typeof(MyEnum).Name;

Here's some examples: 以下是一些例子:

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

.FullName like the example above gives the full type name, including the namespace. 像上面的例子一样, .FullName给出了完整的类型名称,包括命名空间。

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

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