예제 #1
0
        static string GetName(long value, ResourceManager resources, EnumInfo info, bool checkForFlags)
        {
            object obj;

            if (!info.TryGetValue(value, out obj))
            {
                if (checkForFlags)
                {
                    if (value == 0)
                    {
                        return("0");
                    }

                    ulong uvalue  = (ulong)value;
                    var   builder = new StringBuilder(1024);

                    bool  builderEmpty = true;
                    ulong bit          = 1;
                    for (int i = 0; i < sizeof(ulong) * 8; ++i, bit <<= 1)
                    {
                        if ((uvalue & bit) != 0)
                        {
                            if (builderEmpty)
                            {
                                builderEmpty = false;
                            }
                            else
                            {
                                builder.Append(", ");
                            }

                            builder.Append(GetName((long)bit, resources, info, false));
                        }
                    }

                    return(builder.ToString());
                }

                return(value.ToString());
            }

            var keyField = obj as Tuple <string, string>;

            if (keyField == null)
            {
                var plainName = obj as Tuple <string>;
                if (plainName != null)
                {
                    return(plainName.Item1);
                }

                var field = (FieldInfo)obj;
                var attrs = (LocalizedNameAttribute[])field.GetCustomAttributes(s_attrType, false);
                if (attrs.Length == 0)
                {
                    info[value] = new Tuple <string>(field.Name);
                    return(field.Name);
                }

                info[value] = keyField = new Tuple <string, string>(attrs[0].Key, field.Name);
            }

            var result = resources.GetString(keyField.Item1);

            if (string.IsNullOrEmpty(result))
            {
                result = keyField.Item2;
            }
            return(result);
        }