public static string GetScriptAttribute( int enumValue, 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 ];
if ( type == field.FieldType.UnderlyingSystemType &&
(int)field.GetValue( null ) == enumValue )
{
// 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 )
{
// convert the first element to the right type (assume there is only 1 attribute)
ScriptEnumAttribute scriptAtt = (ScriptEnumAttribute)atts[ 0 ];
// if the values match
return scriptAtt.ScriptValue;
} // if
}
}
// invalid
return null;
}