JamesBrownIsDead
JamesBrownIsDead

Reputation: 345

C# enums/reflection

Let's say I have a function that takes a string. That string contains the fully name of an enum type (e.g. "MyCompany.Area.AotherNamespace.MyEnum").

How could I create an array of strings (or List<string>) whose elements are the values of MyCompany.Area.AotherNamespace.MyEnum?

Is that even possible?

I'm basically trying to serialize an enum type serverside and then output it in clientside JavaScript so I don't have to define an enum in two places--in my C# and my JavaScript.

Upvotes: 0

Views: 1391

Answers (1)

Matt Greer
Matt Greer

Reputation: 62027

Type type = Type.GetType(yourStringWithTheFullEnumName);
string[] valueNames = Enum.GetNames(type);

This doesn't work in Silverlight (and possibly the Compact Framework). If you are working in an "alternative" CLR, then you can get the same effect with a tad bit of reflection.

Upvotes: 7

Related Questions