public static object Lookup( string val, Type type )
{
// get the list of fields in the enum
FieldInfo[] fields = type.GetFields();
// loop through each one and see if it is mapped to the supplied value
for ( int i = 0; i < fields.Length; i++ )
{
FieldInfo field = fields[ i ];
// find custom attributes declared for this field
object[] atts = field.GetCustomAttributes( typeof( ScriptEnumAttribute ), false );
// if we found 1, take a look at it
if ( atts.Length > 0 )
{
for ( int index = 0; index < atts.Length; index++ )
{
// convert the first element to the right type (assume there is only 1 attribute)
ScriptEnumAttribute scriptAtt = (ScriptEnumAttribute)atts[ index ];
// if the values match
if ( scriptAtt.ScriptValue.ToLower() == val.ToLower() )
{
// return the enum value for this script equivalent
return Enum.Parse( type, field.Name, true );
}
}
} // if
} // for
// invalid enum value
return null;
}