Inheritance: System.Attribute
コード例 #1
0
        /// <summary>
        ///		Returns an actual enum value for a enum that can be used in script files.
        /// </summary>
        /// <param name="val"></param>
        /// <param name="type"></param>
        /// <returns></returns>
        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)
                {
                    // convert the first element to the right type (assume there is only 1 attribute)
                    ScriptEnumAttribute scriptAtt = (ScriptEnumAttribute)atts[0];

                    // if the values match
                    if (scriptAtt.ScriptValue == val)
                    {
                        // return the enum value for this script equivalent
                        return(Enum.Parse(type, field.Name, true));
                    }
                } // if
            }     // for

            //	invalid enum value
            return(null);
        }
コード例 #2
0
        /// <summary>
        ///		Returns a string describing the legal values for a particular enum.
        /// </summary>
        /// <param name="type"></param>
        /// <returns>
        ///		A string containing legal values for script file.
        ///		i.e. "'none', 'clockwise', 'anticlockwise'"
        /// </returns>
        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)
                {
                    // 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));
            }
        }