开发者

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

开发者 https://www.devze.com 2022-12-28 20:25 出处:网络
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:

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

精彩评论

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

关注公众号