Пример #1
0
        /// <summary>GetString(式木版)</summary>
        /// <typeparam name="T">struct(Enum Field)</typeparam>
        /// <param name="value">値</param>
        /// <returns>列挙型を文字列化</returns>
        public static string ToStringByExpressionTree <T>(this T value) where T : struct
        {
            // Enum Field
            Type type = typeof(T);

            if (type.IsEnum == false)
            {
                throw new ArgumentException("value must be a enum type");
            }
            else
            {
                MulticastDelegate multicastDelegate = null;

                // MulticastDelegateのロード
                if (!EnumToStringByExpressionTreeExtensions.ToStringMethods.TryGetValue(type, out multicastDelegate))
                {
                    // こちらは、FlagsAttributeを処理可能。
                    multicastDelegate = EnumToStringByExpressionTreeExtensions.CreateToString <T>();
                    // MulticastDelegateをキャッシュ
                    EnumToStringByExpressionTreeExtensions.ToStringMethods[type] = multicastDelegate;
                }

                // MulticastDelegateでFastReflection
                Func <T, string> f = (Func <T, string>)multicastDelegate;
                return(f(value));
            }
        }
Пример #2
0
 /// <summary>ToStringByExpressionTree(式木版)</summary>
 /// <typeparam name="T">struct(Enum Field)</typeparam>
 /// <param name="value">値</param>
 /// <returns>列挙型を文字列化</returns>
 public static string ToStringByExpressionTree <T>(this Nullable <T> value) where T : struct
 {
     if (value.HasValue == true)
     {
         return(EnumToStringByExpressionTreeExtensions.ToStringByExpressionTree(value.Value));
     }
     else
     {
         return("");
     }
 }