public static string GetLegalValues( Type type )
{
StringBuilder legalValues = new StringBuilder();
// 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[ 0 ];
// if the values match
legalValues.AppendFormat( "'{0}',", scriptAtt.ScriptValue );
}
} // if
} // for
// return the full string
if ( legalValues.Length == 0 )
return "(No values found for type " + type.Name.ToString() + ")";
else
return legalValues.ToString( 0, legalValues.Length - 1 );
}
}