Example #1
0
 private static string GetDescriptionID(FieldInfo field)
 {
     object[] attrs = field.GetCustomAttributes(typeof(EnumValueAttribute), false);
     if (attrs.Length > 0)
     {
         EnumValueAttribute enumValueAttr = (EnumValueAttribute)attrs[0];
         return(enumValueAttr.Description);
     }
     return(null);
 }
        public void Init(Type t)
        {
            _strResource = null;
            _assembly    = t.Assembly;

            MemberInfo[] ms = t.GetMembers();
            _descToValue = new Hashtable(ms.Length);
            _nameToValue = new Hashtable(ms.Length);

            ArrayList descriptions = new ArrayList(ms.Length);
            ArrayList names        = new ArrayList(ms.Length);

            int expected = 0;

            foreach (MemberInfo mi in ms)
            {
                FieldInfo fi = mi as FieldInfo;
                if (fi != null && fi.IsStatic && fi.IsPublic)
                {
                    int intVal = (int)fi.GetValue(null);                     //int以外をベースにしているEnum値はサポート外
                    if (intVal != expected)
                    {
                        throw new Exception("unexpected enum value order");
                    }
                    EnumValueAttribute a = (EnumValueAttribute)(fi.GetCustomAttributes(typeof(EnumValueAttribute), false)[0]);

                    string desc = a.Description;
                    descriptions.Add(desc);
                    _descToValue[desc] = intVal;

                    string name = fi.Name;
                    names.Add(name);
                    _nameToValue[name] = intVal;

                    expected++;
                }
            }

            _descriptions = (string[])descriptions.ToArray(typeof(string));
            _names        = (string[])names.ToArray(typeof(string));
        }