Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / New Users / February 2007

Tip: Looking for answers? Try searching our database.

Alternative to Enum.IsDefined()

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Rasmus - 07 Feb 2007 15:03 GMT
Due to the poor performance Enum.IsDefined producesi’m looking for an
alternative to this utility method:
Note:  the enums passed to the method will not change during runtime.

public static object EnumTranslate(Type enumType, object enumValue) {
 if (!Enum.IsDefined(enumType, enumValue)) {
   if (Attribute.IsDefined(enumType, typeof(FlagsAttribute))) {
     // just ignore
   } else {
     throw new InvalidEnumArgumentException();
   }
 }
 return Enum.ToObject(enumType, enumValue);
}
Ollie Riches - 07 Feb 2007 17:36 GMT
if it does give bad performance as you suggest why don't you cache the
result if the value of enumValue is repeative...

HTH

Ollie Riches

> Due to the poor performance Enum.IsDefined producesi'm looking for an
> alternative to this utility method:
[quoted text clipped - 10 lines]
>  return Enum.ToObject(enumType, enumValue);
> }
Rasmus - 07 Feb 2007 17:57 GMT
Good idea, i'll give it a try

/Rasmus

> if it does give bad performance as you suggest why don't you cache the
> result if the value of enumValue is repeative...
[quoted text clipped - 17 lines]
> >  return Enum.ToObject(enumType, enumValue);
> > }

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.