Пример #1
0
        static CacheEntry CreateCacheEntry(Type enumType)
        {
            CacheEntry entry;

            // First get the Resource Manager.

            var attrs = (LocalizedNameContainerAttribute[])
                        enumType.GetCustomAttributes(s_attrContnrType, false);

            if (attrs.Length == 0)
            {
                s_cache[enumType] = entry = new CacheEntry(null, null, false);
                return(entry);
            }

            var container = attrs[0].ResourceManager;
            var method    = container.GetProperty("ResourceManager",
                                                  BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic, null,
                                                  s_resMgrType, Type.EmptyTypes, new ParameterModifier[0]);

            if (method == null)
            {
                throw new InvalidOperationException("Cannot find ResourceManager in type " + container);
            }

            var resMgr = (ResourceManager)method.GetValue(null, null);

            if (resMgr == null)
            {
                throw new InvalidOperationException("Failed to access ResourceManager in " + container);
            }

            // Resource Manager retrieved, now get the fields.

            var fields = enumType.GetFields(BindingFlags.Public | BindingFlags.Static);
            var ei     = new EnumInfo();

            bool isULong = enumType.GetEnumUnderlyingType() == typeof(ulong);

            foreach (var field in fields)
            {
                var obj = (IConvertible)field.GetRawConstantValue();

                long value;
                if (isULong)
                {
                    value = (long)(ulong)obj;
                }
                else
                {
                    value = obj.ToInt64(null);
                }

                // Sometimes there are two fields with the same value
                if (ei.ContainsKey(value))
                {
                    continue;
                }

                ei.Add(value, field);
            }

            // Cache the found stuff

            s_cache[enumType] = entry = new CacheEntry(resMgr, ei, enumType.IsDefined(s_attrFlagsType, false));
            return(entry);
        }