Thoughts from Dan Miser RSS 2.0
# Thursday, March 22, 2007
The following application shows a subtle difference in dealing with enums using Enum class methods and when using reflection. In this sample, I am looking to find the number of elements in the enum. Enum.GetValues and Enum.GetNames are not implemented in the .NET Compact Framework, so I cannot use those methods.

By using reflection, I figured I would gain the upper hand and take control of my enum woes. Unfortunately, calling GetFields on an enum type adds an extra entry named "value__" to the returned list. After browsing through the decompilation of Enum, I found that value__ is just a special instance field used by the enum to hold the value of the selected member. I also noticed that the actual enum members are really marked as static. So, to get around this problem, all you need to do is call GetFields with the BindingFlags set to only retrieve the public, static fields (see the code example below).


using System;

using System.Reflection;

 

namespace EnumReflection

{

    class Program

    {

        enum Test

        {

            One,

            Two,

            Three

        }

 

        static void Main(string[] args)

        {

            Type enumType = typeof(Test);

 

            //The following prints 3, but we can't use this method in .NET CF

            Console.WriteLine(Enum.GetValues(enumType).Length);

 

            FieldInfo[] infos;

            infos = enumType.GetFields();

 

            //The following prints 4! There is an extra, unrelated Int32 entry named "value__"

            //at the zeroth element of the info array.

            Console.WriteLine(infos.Length);

 

            //The following prints the 3 enum elements. The value__ field is removed by

            //specifying we only want the public, static fields.

            infos = enumType.GetFields(BindingFlags.Public | BindingFlags.Static);

            foreach (FieldInfo fi in infos)

            {

                Console.WriteLine(fi.Name);

            }

        }

    }

}

Thursday, March 22, 2007 4:16:48 PM (GMT Standard Time, UTC+00:00)  #    Comments [0] -
.NET | Compact Framework
Tracked by:
https://graviton.eglin.af.mil:443/SCWCS/blogs/hulkers/archive/2007/03/28/Links-_... [Pingback]
http://www.cinnug.org/blogs/memberblogs/archive/2007/03/22/links-3-22-2007.aspx [Pingback]
http://9nq-information.info/98705330/index.html [Pingback]
http://9np-information.info/26611210/reviews-of-smart-dvd-creator-pro.html [Pingback]
http://9nm-information.info/14277338/index.html [Pingback]
http://9nf-information.info/40651007/index.html [Pingback]
http://9nb-information.info/85100311/cars-tv-tray.html [Pingback]
http://9na-information.info/22194758/index.html [Pingback]
http://9ns-information.info/77732833/pokemon-card-appraisals.html [Pingback]
http://9no-information.info/16154486/index.html [Pingback]
http://9nl-information.info/35721720/index.html [Pingback]
http://9ng-information.info/91424458/la-cooking.html [Pingback]
http://9nn-information.info/27326253/used-furniture-in-toledo-ohio.html [Pingback]
http://9nk-information.info/37057667/harnessed-women-racing.html [Pingback]
http://9qi-information.info/38409511/codice-deontologico-forense.html [Pingback]
http://9om-information.info/96701805/island-creek-sporting-clay.html [Pingback]
http://9ol-information.info/18083020/index.html [Pingback]
http://9on-information.info/13342996/index.html [Pingback]
http://9qg-information.info/35884873/index.html [Pingback]
http://9oe-information.info/70635155/index.html [Pingback]
http://9qe-information.info/48016646/index.html [Pingback]
http://9rv-information.info/06101500/index.html [Pingback]
http://9rb-information.info/30391388/index.html [Pingback]
http://9rv-information.info/93756569/index.html [Pingback]
http://9sk-information.info/08817565/index.html [Pingback]
http://9sb-information.info/16633745/index.html [Pingback]
http://9rq-information.info/63123092/dravid-house-destroy.html [Pingback]
http://9ri-information.info/52515758/star-wars-role-playing-game-character-sheet... [Pingback]
http://9uael-le-informazioni.info/48322933/index.html [Pingback]
http://9uafh-le-informazioni.info/79840771/index.html [Pingback]
http://9uaet-le-informazioni.info/16006624/index.html [Pingback]
http://9uafh-le-informazioni.info/53129398/index.html [Pingback]
http://9uaet-le-informazioni.info/41884612/index.html [Pingback]
http://9uafe-le-informazioni.info/71449935/estima.html [Pingback]
http://9uafp-le-informazioni.info/64778978/centro-benessere-brescia-beauty-farm.... [Pingback]
http://9uagk-le-informazioni.info/99364971/index.html [Pingback]
http://9uahe-le-informazioni.info/74611070/consigli-coltivazione.html [Pingback]
http://9uahb-le-informazioni.info/64074616/index.html [Pingback]
http://9uahn-le-informazioni.info/96952263/netmeeting-port-forwarding.html [Pingback]
http://9uahe-le-informazioni.info/17432835/index.html [Pingback]
http://9uaha-le-informazioni.info/81416655/index.html [Pingback]
http://9uagq-le-informazioni.info/81613868/funziona-cpap.html [Pingback]
http://kovjara.com/2007/06/03/reflections-on-a-missed-blogging-conference-in-syd... [Pingback]
http://thecanabible.com/2007/02/04/hearst-argyle-ups-ante/ [Pingback]
http://pinemi.com/2007/06/03/how-the-weltgef-does/ [Pingback]
http://nakrema.com/2007/06/03/metallklumpen-it-horizontal/ [Pingback]
http://sinuta.com/2007/06/03/pomomusings/ [Pingback]
http://lenopa.com/2007/06/03/us-50-lawrence-county/ [Pingback]
http://azinna.com/2007/06/03/auf-klebeband-reprasentant-last-kunden-aol-nicht-be... [Pingback]
http://disola.com/2007/06/03/leute-sprechen/ [Pingback]
http://josephkonopka.com/2007/03/08/are-the-conservative-jews-political-by-the-j... [Pingback]
http://pimpasa.com/2007/06/03/nanotechnology-to-over-compress-to-the-internets/ [Pingback]
http://gajipa.com/2007/06/03/change-are-you-not-it/ [Pingback]
http://sakmara.com/2007/06/03/mr-peanut-of-taken-prisoners/ [Pingback]
http://lenopa.com/2007/06/03/parenting-sense-cheat/ [Pingback]
http://begagy.com/2007/06/03/a-four-weekly-waiting-period-for-cancer-treatment-a... [Pingback]
http://sakmara.com/2007/06/03/douz-international-saharan-festival-2004/ [Pingback]
http://kemifa.com/2007/06/03/which-happened-when-etailers-into-blogs-into-podcas... [Pingback]
http://thecanabible.com/2007/02/04/the-demo-do-quite/ [Pingback]
http://pimpasa.com/2007/06/03/reflections-on-300-examples/ [Pingback]
http://lenopa.com/2007/06/03/tax-assessment-of-of-questions/ [Pingback]
http://kemifa.com/2007/06/03/pruefenmeme-propagation-in-blogspace-add-your-blog/ [Pingback]
http://pinemi.com/2007/06/03/j-renee-5519b/ [Pingback]
http://nakrema.com/2007/06/03/university-start-for-sales/ [Pingback]
http://gspsf-happyholidays.com/2007/06/12/blogoff-post-12-web-log-report-sellsiu... [Pingback]
http://artforreal.com/2007/03/08/back-of-israel/ [Pingback]
http://nasroka.com/2007/06/03/present-the-crm-customer-experience-of-improvement... [Pingback]
http://executivefitnessmaa.com/2007/03/08/these-web-logs-entry/ [Pingback]
http://chicopaddleheads.com/2007/06/12/voice-now/ [Pingback]
http://azinna.com/2007/06/03/so-well/ [Pingback]
http://oknama.com/2007/06/03/you-leave-a-view-under-their-to-us-cap-have/ [Pingback]
http://pinemi.com/2007/06/03/fema-director-michael-brown-relieved-of-tasks-of-hu... [Pingback]
http://stapita.com/2007/06/03/nachrichten-kugeln-gerade-ein-frickin-tag-zum-bere... [Pingback]
http://3rdcoaststudios.com/wp-login.php?redirect_to=http://3rdcoaststudios.com/2... [Pingback]
http://windmillsonline.us/2007/03/08/question-you-would-prefer-a/ [Pingback]
http://kemifa.com/2007/06/03/patrick-co-gruender-von-greenpeace/ [Pingback]
http://cefana.com/2007/06/03/friday-night-separation-lap-keeley-hazell/ [Pingback]
http://gajipa.com/2007/06/03/they-caught-my-kind/ [Pingback]
http://thecanabible.com/2007/02/04/britblog-summary-88/ [Pingback]
http://senkara.com/2007/06/03/icann-board-meeting-july-11-1-2005/ [Pingback]
http://thepowerbox.com/2007/06/12/mixing-feelings/ [Pingback]
http://kligena.com/2007/06/03/assistance-i-have-the-cover-of-mine-tech-geek-geek... [Pingback]
http://avinnovators.com/2007/03/08/forecast-this/ [Pingback]
http://yabola.com/2007/06/03/currentculture/ [Pingback]
http://senkara.com/2007/06/03/leaptag-yoriwas/ [Pingback]
http://executivefitnessmaa.com/2007/03/08/fundamentalist-christian-abuse-bible-a... [Pingback]
http://callsmile.com/2007/03/08/skype-remindme/ [Pingback]
http://midiso.com/2007/06/03/mizuno-wave-passenger-8/ [Pingback]
http://tanola.com/2007/06/03/wtw-wifely-expectations/ [Pingback]
http://kigyga.com/2007/06/03/warumsind-ntfs-and-researcher-on-the-file-name-sort... [Pingback]
http://stejala.com/2007/06/03/barrier-street-krieger-episode-3-postmortem/ [Pingback]
http://gajipa.com/2007/06/03/polizia-lamborghini/ [Pingback]
http://disola.com/2007/06/03/neu/ [Pingback]
http://pinemi.com/2007/06/03/stupid-contracts-eliminating-possible-customers/ [Pingback]
http://sakmara.com/2007/06/03/the-long/ [Pingback]
http://oknama.com/2007/06/03/message-answer-process-of-dad-john-in-the-spiess-co... [Pingback]
http://artforreal.com/2007/03/08/of-podcasts-and-of-the-magazines/ [Pingback]
http://kemifa.com/2007/06/03/doogie-data-base-search-routine/ [Pingback]
http://thecanabible.com/2007/02/04/transparency-objectivity-and-independence-una... [Pingback]
http://rojara.com/2007/06/03/apples-anonymous-blogger/ [Pingback]
http://kigyga.com/2006/11/04/long-termersatzmann-mr-hussongs-employed-in-stead/ [Pingback]
http://kligena.com/2007/06/03/raich-day-of-the-activity-wed-june-8-with-your-rep... [Pingback]
http://nasroka.com/2007/06/03/friends-and-their-computers-necessities/ [Pingback]
http://thecanabible.com/2007/02/04/blogging-on-line-retailer/ [Pingback]
http://sinuta.com/2007/06/03/virtualdub-169-released-in-archives/ [Pingback]
http://kligena.com/2007/06/03/zentrale-planung-oder-organisches-wachstum/ [Pingback]
http://yabola.com/2007/06/03/things-straight-stop/ [Pingback]
http://wildroadtrip.com/2007/03/08/techsmith-they-receive-it/ [Pingback]
http://zabity.com/2007/06/03/do-not-prepare-or/ [Pingback]
http://kemifa.com/2007/06/03/system-put-to-brasil-in-here-dragging-and-drops-a-b... [Pingback]
Navigation
Archive
<December 2014>
SunMonTueWedThuFriSat
30123456
78910111213
14151617181920
21222324252627
28293031123
45678910
Blogroll
About the author/Disclaimer

Disclaimer
The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.

© Copyright 2014
Dan Miser
Sign In
Statistics
Total Posts: 388
This Year: 0
This Month: 0
This Week: 0
Comments: 630
Themes
Pick a theme:
All Content © 2014, Dan Miser
DasBlog theme 'Business' created by Christoph De Baene (delarou)