Ejemplo n.º 1
0
        /// <summary>
        /// Gets Expression for Enum to string conversion.
        /// </summary>
        /// <typeparam name="TEnum">Enum type. May be nullable.</typeparam>
        /// <param name="toString">Enum to string conversion delegate.</param>
        /// <param name="nullEnumString">String for null enum value.</param>
        /// <param name="zeroEnumString">String for 0 enum value.</param>
        /// <param name="delimiter">Delimiter for [Flags] enum.</param>
        /// <returns></returns>
        public static Expression <Func <TEnum, string> > GetEnumToStringExpression <TEnum>(Func <Enum, string> toString, string nullEnumString = null, string zeroEnumString = "", string delimiter = ", ")
        {
            Type enumType = typeof(TEnum);

            Type enumExactType    = enumType;
            Type nullableEnumType = Nullable.GetUnderlyingType(enumExactType);

            if (nullableEnumType != null)
            {
                enumExactType = nullableEnumType;
            }
            Type enumUnderlyingType = Enum.GetUnderlyingType(enumExactType);

            var allMembers = EnumExtensions.GetAllMembers(enumExactType)
                             .Select(e => new KeyValuePair <Enum, string>(e, toString(e)))
                             .OrderBy(e => e.Value)
                             .ToDictionary(e => e.Key, e => e.Value);

            object zero        = Activator.CreateInstance(enumUnderlyingType);
            Enum   zeroEnumVal = (Enum)Enum.ToObject(enumExactType, zero);

            Expression zeroStringExpression;

            if (zeroEnumString == "")
            {
                string zeroString;
                if (allMembers.TryGetValue(zeroEnumVal, out zeroString))
                {
                    allMembers.Remove(zeroEnumVal);
                    zeroStringExpression = Expression.Constant(zeroString, StringType);
                }
                else
                {
                    zeroStringExpression = EmptyStringExpression;
                }
            }
            else
            {
                if (allMembers.ContainsKey(zeroEnumVal))
                {
                    allMembers.Remove(zeroEnumVal);
                }

                zeroStringExpression = Expression.Constant(zeroEnumString, StringType);
            }

            Expression zeroConstant = Expression.Constant(zeroEnumVal, enumType);//Expression.Convert(Expression.Constant(zero, enumUnderlyingType), enumType);

            Expression nullStringExpression = nullEnumString == null ? NullStringExpression : Expression.Constant(nullEnumString, StringType);

            //Flag enum.
            if (enumExactType.GetCustomAttributes(typeof(FlagsAttribute), false).Length != 0)
            {
                return(GetFlagEnumToStringExpression <TEnum>(allMembers, enumType, enumExactType, enumUnderlyingType, zeroConstant, zeroStringExpression, nullStringExpression, delimiter));
            }
            else
            {
                return(GetEnumToStringExpression <TEnum>(allMembers, enumType, enumExactType, zeroConstant, zeroStringExpression, nullStringExpression));
            }
        }