Home All Groups Group Topic Archive Search About

Alternative to Enum.IsDefined()

Author
7 Feb 2007 3:03 PM
Rasmus
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);
}

Author
7 Feb 2007 5:36 PM
Ollie Riches
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

Show quote
"Rasmus" <Rasmus@newsgroup.nospam> wrote in message
news:6A9B1C14-0EBC-4ACA-BDEC-0363566F5275@microsoft.com...
>
> 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);
> }
>
>
>
>
Author
7 Feb 2007 5:57 PM
Rasmus
Good idea, i'll give it a try

/Rasmus



Show quote
"Ollie Riches" wrote:

> 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
>
> "Rasmus" <Rasmus@newsgroup.nospam> wrote in message
> news:6A9B1C14-0EBC-4ACA-BDEC-0363566F5275@microsoft.com...
> >
> > 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);
> > }
> >
> >
> >
> >
>
>
>

AddThis Social Bookmark Button