2006-02-06

FuzzyEnumParse

This function takes an Enum type and a text representing the value of the enum, and if the Enum has a field with a name contained in the text (enumText), then it returns that value.

For use when for instance the database field to parse from has been extended somewhat... Modify to suit your needs!


private object FuzzyEnumParse(System.Type type, string enumText)
{
System.Reflection.FieldInfo[] fis = type.GetFields();
foreach(System.Reflection.FieldInfo fi in fis)
{
if(fi.Name!="value__")
{
object obj=fi.GetValue(null);
if(enumText.IndexOf(obj.ToString())>-1)
{
return obj;
}
}
}
return null;
}

No comments:

Post a Comment