public override string ToString()
{
  Type type = base.GetType();
  object obj2 = ((RtFieldInfo) GetValueField(type)).InternalGetValue(this, false);
  return InternalFormat(type, obj2);
}
* This source code was highlighted with Source Code Highlighter.This issue could be fixed with the following trick code:
static class EnumHelper
{
  static class Cache<T>
  {
    internal static readonly Dictionary<T, string> Values = new Dictionary<T, string>();
  }
    
  public static string FastToString<T>(this T @enum) where T:struct
  {  
    if (!typeof(T).IsEnum)
      throw new ArgumentException(string.Format("Type {0} is not an enumeration.", typeof(T)));
    lock (Cache<T>.Values)
    {
      string result;
      if (!Cache<T>.Values.TryGetValue(@enum, out result))
      {
        result = @enum.ToString();
        Cache<T>.Values.Add(@enum, result);
      }
      return result;
    }
  }
}
* This source code was highlighted with Source Code Highlighter. 
 
если сделать через статический конструктор, lock не нужен
ReplyDeleteВ смысле - в статик конструкторе забивать все значения? Да, тогда лок не нужен, но кэш не будет Lazy.
ReplyDeleteА зачем он lazy?
ReplyDeleteHi! In the following article I tried to do pretty much the same optimization, but make it work for more cases and optimize some more. http://www.codeproject.com/KB/dotnet/enum.aspx
ReplyDeleteIs this thread-safe?
ReplyDeleteAs far as Cache values are used under lock, this solution is thread safe.
Delete