开发者

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 "MyE开发者_StackOverflownum"

typeof(MyEnum) mostly works, however the namespace of the enumeration is appended to the front.

Any help would be appreciated... thanks!


Use .Name to get only the type in the string, like this:

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜