/// <summary>GetString(Emit版)</summary> /// <typeparam name="T">struct(Enum Field)</typeparam> /// <param name="value">値</param> /// <returns>列挙型を文字列化</returns> public static string ToStringByEmit <T>(this Nullable <T> value) where T : struct { if (value.HasValue == true) { return(EnumToStringByEmitExtensions.ToStringByEmit(value.Value)); } else { return(""); } }
/// <summary>GetString(Emit版)</summary> /// <typeparam name="T">struct(Enum Field)</typeparam> /// <param name="value">値</param> /// <returns>列挙型を文字列化</returns> public static string ToStringByEmit <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 (!EnumToStringByEmitExtensions.ToStringMethods.TryGetValue(type, out multicastDelegate)) { if (type.GetCustomAttributes(typeof(FlagsAttribute), false).Length == 0) { // FlagsAttributeが無い場合、 // MulticastDelegateを生成し、 multicastDelegate = EnumToStringByEmitExtensions.CreateToString <T>(); // MulticastDelegateをキャッシュ EnumToStringByEmitExtensions.ToStringMethods[type] = multicastDelegate; } else { // FlagsAttributeが有る場合、 // MulticastDelegateを生成できないので、 // 単なるToString()。// コレが遅いらしい。 return(value.ToString().Replace(" ", "")); } } // MulticastDelegateでFastReflection Func <T, string> f = (Func <T, string>)multicastDelegate; return(f(value)); } }