示例#1
0
        public static string ToShowText(this Enum en, bool exceptionIfFail, string flagsSeparator)
        {
            string enumFullName = EnumExtensions.GetEnumFullName(en);
            string showText     = default(string);

            if (!EnumExtensions._cacheEnumShowTextdDic.TryGetValue(enumFullName, out showText))
            {
                Type     type             = en.GetType();
                object[] customAttributes = type.GetCustomAttributes(typeof(FlagsAttribute), false);
                if (customAttributes != null && customAttributes.Length != 0)
                {
                    long          num           = Convert.ToInt64(en);
                    StringBuilder stringBuilder = new StringBuilder();
                    string[]      names         = Enum.GetNames(type);
                    string        result        = "";
                    string[]      array         = names;
                    foreach (string text in array)
                    {
                        long num2 = Convert.ToInt64(Enum.Parse(type, text));
                        if (num2 == 0)
                        {
                            object[] customAttributes2 = type.GetField(text).GetCustomAttributes(typeof(EnumShowTextAttribute), false);
                            if (customAttributes2.Length != 0)
                            {
                                result = ((EnumShowTextAttribute)customAttributes2[0]).ShowText;
                            }
                        }
                        else if ((num2 & num) == num2)
                        {
                            if (stringBuilder.Length != 0)
                            {
                                stringBuilder.Append(flagsSeparator);
                            }
                            object[] customAttributes3 = type.GetField(text).GetCustomAttributes(typeof(EnumShowTextAttribute), false);
                            if (customAttributes3.Length != 0)
                            {
                                stringBuilder.Append(((EnumShowTextAttribute)customAttributes3[0]).ShowText);
                            }
                            else
                            {
                                if (exceptionIfFail)
                                {
                                    throw new InvalidOperationException($"此枚举{enumFullName}未定义EnumShowTextAttribute");
                                }
                                stringBuilder.Append(text);
                            }
                        }
                    }
                    if (stringBuilder.Length > 0)
                    {
                        return(stringBuilder.ToString());
                    }
                    return(result);
                }
                FieldInfo field = type.GetField(en.ToString());
                if (field == (FieldInfo)null)
                {
                    throw new InvalidOperationException($"非完整枚举{enumFullName}");
                }
                object[] customAttributes4 = field.GetCustomAttributes(typeof(EnumShowTextAttribute), false);
                if (customAttributes4.Length != 0)
                {
                    showText = ((EnumShowTextAttribute)customAttributes4[0]).ShowText;
                    lock (EnumExtensions._cacheEnumShowTextdDic)
                    {
                        EnumExtensions._cacheEnumShowTextdDic[enumFullName] = showText;
                    }
                    goto IL_0265;
                }
                if (exceptionIfFail)
                {
                    throw new InvalidOperationException($"此枚举{enumFullName}未定义EnumShowTextAttribute");
                }
                return(en.ToString());
            }
            goto IL_0265;
IL_0265:
            return(showText);
        }
示例#2
0
        public static string ToShowText(this System.Enum en, bool exceptionIfFail, string flagsSeparator)
        {
            string enumFullName = EnumExtensions.GetEnumFullName(en);
            string showText;
            string result;

            if (!EnumExtensions._cacheEnumShowTextdDic.TryGetValue(enumFullName, out showText))
            {
                System.Type type             = en.GetType();
                object[]    customAttributes = type.GetCustomAttributes(typeof(System.FlagsAttribute), false);
                if (customAttributes != null && customAttributes.Length > 0)
                {
                    long num = System.Convert.ToInt64(en);
                    System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
                    string[] names = System.Enum.GetNames(type);
                    string   text  = "";
                    string[] array = names;
                    for (int i = 0; i < array.Length; i++)
                    {
                        string text2 = array[i];
                        long   num2  = System.Convert.ToInt64(System.Enum.Parse(type, text2));
                        if (num2 == 0L)
                        {
                            object[] customAttributes2 = type.GetField(text2).GetCustomAttributes(typeof(EnumShowTextAttribute), false);
                            if (customAttributes2.Length > 0)
                            {
                                text = ((EnumShowTextAttribute)customAttributes2[0]).ShowText;
                            }
                        }
                        else if ((num2 & num) == num2)
                        {
                            if (stringBuilder.Length != 0)
                            {
                                stringBuilder.Append(flagsSeparator);
                            }
                            object[] customAttributes2 = type.GetField(text2).GetCustomAttributes(typeof(EnumShowTextAttribute), false);
                            if (customAttributes2.Length > 0)
                            {
                                stringBuilder.Append(((EnumShowTextAttribute)customAttributes2[0]).ShowText);
                            }
                            else
                            {
                                if (exceptionIfFail)
                                {
                                    throw new System.InvalidOperationException(string.Format("此枚举{0}未定义EnumShowTextAttribute", enumFullName));
                                }
                                stringBuilder.Append(text2);
                            }
                        }
                    }
                    if (stringBuilder.Length > 0)
                    {
                        result = stringBuilder.ToString();
                        return(result);
                    }
                    result = text;
                    return(result);
                }
                else
                {
                    System.Reflection.FieldInfo field = type.GetField(en.ToString());
                    if (field == null)
                    {
                        throw new System.InvalidOperationException(string.Format("非完整枚举{0}", enumFullName));
                    }
                    object[] customAttributes2 = field.GetCustomAttributes(typeof(EnumShowTextAttribute), false);
                    if (customAttributes2.Length > 0)
                    {
                        showText = ((EnumShowTextAttribute)customAttributes2[0]).ShowText;
                        lock (EnumExtensions._cacheEnumShowTextdDic)
                        {
                            EnumExtensions._cacheEnumShowTextdDic[enumFullName] = showText;
                        }
                    }
                    else
                    {
                        if (exceptionIfFail)
                        {
                            throw new System.InvalidOperationException(string.Format("此枚举{0}未定义EnumShowTextAttribute", enumFullName));
                        }
                        result = en.ToString();
                        return(result);
                    }
                }
            }
            result = showText;
            return(result);
        }