Alternative to Enum.IsDefined()

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

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);
}
 
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
 
Good idea, i'll give it a try

/Rasmus



Ollie Riches said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top