JoelDixon.com

"Gee, you think? You think that maybe I should use these clamps that I use every day at every opportunity? You're a freaking genius, you idiot!" - Clamps, Futurama 2ACV13

Blog
General
Travel
Hockey
Technical
Gaming
Catalogue
Movies
TV Shows
Music
Video Games
Stats
Pages
Travel Plans
Hockey Games
Live Music
Phone History
Monument Mounting
Things to Do
Links
About


Blog Archive
Current Blogs
2017 (12)
2016 (1)
2015 (24)
2014 (1)
2013 (4)
2012 (39)
2011 (2)
2010 (18)
2009 (28)
2008 (25)
2007 (67)
2006 (23)
2005 (32)
2004 (10)

Blog Tags
General (13)
10 in '10 (4)
Books (1)
DVDs (3)
Funny (13)
Gadgets (5)
Hawt (1)
Movies (14)
Music (7)
Pets (4)
Recipe (17)
Running (3)
TV Shows (2)
Web (29)

Travel (80)
Akihabara (1)
Amsterdam (1)
Argentina (2)
Asakusa (1)
Barcelona (1)
Berlin (1)
Brussels (1)
Budapest (1)
Buenos Aires (1)
Cairo (1)
Calgary (2)
Chicago (1)
Cusco (1)
Dijon (1)
Dresden (1)
Edinburgh (1)
Edmonton (12)
Florence (1)
Gero (1)
Ginza (1)
Harajuku (1)
Himeji (1)
Hiroshima (1)
Iga (1)
inca-trail (1)
Ireland (1)
Japan (22)
Kamakura (1)
Kawaguchiko (1)
Killarney (1)
Kobe (1)
Krakow (1)
Kuala Lumpur (1)
Kyoto (1)
Lima (1)
Lisbon (1)
London (1)
Lyon (1)
Madrid (1)
Meguro (1)
Miami (1)
Milan (1)
Montreal (1)
Nagoya (1)
Naples (1)
Naples (FL) (1)
Nara (1)
New York (9)
Niagara Falls (3)
Nice (1)
Nikko (1)
Nile Cruise (1)
Odaiba (1)
Osaka (1)
Ottawa (1)
Paris (1)
Philadelphia (1)
Phuket (2)
Pittsburgh (12)
Porto (1)
Prague (1)
Provins (1)
Quebec City (1)
Rome (1)
Sakura (1)
Shibuya (1)
Shinjuku (1)
Takayama (1)
Tampa (1)
Toledo (1)
Toronto (1)
Ueno (1)
Valencia (1)
Vancouver (1)
Venice (1)
Vienna (1)
Warsaw (1)
Washington (4)
Whistler (1)
Yokohama (1)

Hockey (3)
Lemieux Fantasy Camp (4)
Pittsburgh Penguins (31)
Wolverines (1)

Technical (4)
.NET (3)
Java (4)
Software (3)
Work (2)

Gaming (6)
Commodore 64 (2)
Master System (1)
Mega Drive (1)
Rock Band (1)
Xbox 360 (16)
XNA (1)

The rarely updated blog of Joel Dixon

Viewing blogs tagged XNA

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.



Comments (0) | Add Comment

Blog Search

Advanced Search


Recent Blogs
Pittsburgh - For some hockey (why not?), 2017
Philadelphia - Not that sunny, 2017
Miami, Naples and Tampa - Beaches, Babe and a Baby Sloth, 2017
New York - Third time's a charmed lamp, 2017
Pittsburgh Return - Fleury, Fleury, Fleury!, 2017
Chicago - Blackhawks, Bulls and a bean, 2017
Mario Lemieux Fantasy Camp 2017 - Day 4 and 5 (Final Games)
Mario Lemieux Fantasy Camp 2017 - Day 3 (Outdoor Game, Injury, Dinner and Drinks)
Marc-Andre Fleury still giving high fives to his injured buddy
Mario Lemieux Fantasy Camp 2017 - Day 2 (Contract Signing, Training and Game)

Feed
Subscribe to feed Blog Entries

Add to Google
Add to My Yahoo!

Recent Comments
The post about nothing
posted 8 months ago by joeldixon
woow indeed - Very pleased to hear it, my dear friend! link

The post about nothing
posted 8 months ago by adriana
woow - I'm bound to have to agree with your opinion, my friend! link

Pittsburgh Arrival - Border Security Magic, 2017
posted 2 years ago by joeldixon
Welcome Mac - Love to see you here Mac! Speaking of the ice - shame that your big break ... link

Pittsburgh Arrival - Border Security Magic, 2017
posted 2 years ago by MacBeltKarate
Bad-ass Trip Bro - Woah dude! You played hockey with Mario Lemieux!? That bozo is up there as one of ... link

Living up to my title
posted 2 years ago by joeldixon
Yeah, I don't think it's working link


Comment Standings
1. the man with no name (45)
2. Hoff (39)
3. Hitman (33)
4. Gav (27)
5. Brad (24)
6. Eryc-Ads (13)
7. milly (7)
8. Deep Lurker (6)
9. thefury (5)
10. Dieter (4)
About This Site | Contact Me