JoelDixon.com

"Now he is trapped in my book, a crummy world of plot holes and spelling errors!" - Philip J. Fry | "The Big Brain am winning again! I am the greetest! I wil now leave earth for no aparent rasin!" - Master Brain, Futurama 3ACV07

Blog
General
Travel
Sport
Hockey
Technical
Gaming
Catalogue
Movies
TV Shows
Music
Video Games
Stats
Pages
Random Lists
Links
About


Blog Archive
Current Blogs
July 2010 (1)
June 2010 (2)
May 2010 (2)
April 2010 (3)
March 2010 (1)
February 2010 (3)
January 2010 (5)
2009 (28)
2008 (25)
2007 (67)
2006 (23)
2005 (32)
2004 (10)

Blog Tags
General
10 in '10 (4)
Books (1)
DVDs (2)
Funny (13)
Gadgets (5)
Hawt (1)
Movies (13)
Music (6)
Pets (4)
Recipe (16)
TV Shows (1)
Web (29)
Travel
Calgary (2)
Edmonton (12)
Kuala Lumpur (1)
New York (7)
Niagara Falls (2)
Phuket (2)
Pittsburgh (5)
Washington (4)
Sport
Basketball (1)
Running (3)
Hockey
Pittsburgh Penguins (25)
Technical
.NET (3)
Java (4)
Software (3)
Work (2)
Gaming
Commodore 64 (2)
Master System (1)
Mega Drive (1)
Xbox 360 (15)
XNA (1)

GamerTag


The rarely updated blog of Joel Dixon
< Tappy tap tap taparoo | Rafael Nadal is a shaved monkey >

Sunday, February 01, 2009

Enum Members and the .NET Compact Framework

# Posted by Joel Dixon at 01/02/2009 16:37:16

When working on my latest weekend coding project, I am occasionally limited by the methods offered (and not offered) by the .NET Compact Framework (XNA projects are compiled against the compact framework). Usually these limitations are pretty minor (i.e. as TryParse is not available, I need to put a try-catch block around a regular Parse call).

Today I stumbled over another unavailable method - Enum.GetNames(). The GetNames method is called when using Reflection to determine, at runtime, the elements in an enumeration. It took a bit of hunting and experimenting - but here's a Compact Framework-friendly method of achieving the same thing:

C# Code:

enum ShakeFlavour
{
Chocolate,
Strawberry,
Vanilla
}

Type type = typeof(ShakeFlavour);

// Print the list of enum elements - Public and Static BindingFlags required
// to skip internal "value__" field
FieldInfo[] fields = type.GetFields(BindingFlags.Public | BindingFlags.Static);
foreach (FieldInfo info in fields)
{
Console.WriteLine(info.Name);
}

// Parse an enum element from a string
string myFlavour = "Strawberry";
try
{
ShakeFlavour enumObject = (ShakeFlavour)Enum.Parse(type, myFlavour, true);
Console.WriteLine("Shake Flavour: {0}", enumObject);
}
catch (Exception ex)
{
Console.WriteLine("enum element not recognised");
}

It's pretty good that Reflection is included in the compact framework - but it's slightly surprising that they couldn't fit in the Enum.GetNames() method as well.


Back to Top

Comments

No comments have been made.

You must be logged in to comment
Blog Search

Advanced Search


Recent Blogs
Phuket, Thailand - 2010
Mr. Potato Head Mashups
Random Travel #2 - Thailand
You have been the ones, you have been the ones for me.
Balls
Caramelised Pumpkin Risotto
Malaysian Vegetable Curry
Kuala Lumpur, Malaysia - 2010
Yeah ... Baby!
xkcd Parody

Feed
Subscribe to feed Blog Entries

Add to Google
Add to My Yahoo!
Add to Netvibes

Recent Comments
Balls
posted 4 months ago by joeldixon
Exactly. What's wrong with the kids today when they can't spell genitals? And you and I have been using ... link

Balls
posted 4 months ago by abrereton
I'm just glad that they spelt it correctly in the end. link

Kuala Lumpur, Malaysia - 2010
posted 5 months ago by Aland84
Looks like you had a ball at KL. There are plenty of hotels in Kuala Lumpur in the $140 a ... link

Kuala Lumpur, Malaysia - 2010
posted 5 months ago by joeldixon
Definitely, you have to let us know when you're free for UBs! link

Kuala Lumpur, Malaysia - 2010
posted 5 months ago by Hitman
Awesome - Dixon, Awesome mate, living life to the max. You'll have to fill me in more next ... link


Comment Standings
1. the man with no name (45)
2. Hoff (39)
3. Gav (27)
4. Hitman (26)
5. Brad (22)
6. Eryc-Ads (13)
7. Deep Lurker (6)
8. thefury (5)
9. Dieter (4)
10. Zelks (4)

About This Site | Contact Me | 37 days until then