tags:

views:

48

answers:

1

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.

Any help would be appreciated... thanks!

+7  A: 

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.

Nick Craver
Perfect thanks... I didn't know typeof() had so many nifty properties. I didn't even think to check the intellisense for it haha.
Polaris878
@Polaris - When you're calling it you're getting a [`System.Type`](http://msdn.microsoft.com/en-us/library/1ek3kwtc.aspx) so anything you want to use it for :)
Nick Craver