title: Caching reflection data
body: <p>What's the best way to cache expensive data obtained from reflection? For example most fast serializers cache such information so they don't need to reflect every time they encounter the same type again. They might even generate a dynamic method which they look up from the type. </p>
  
  <h2>Before .net 4</h2>
  
  <p>Traditionally I've used a normal static dictionary for that. For example:</p>
  
  <pre><code>private static ConcurrentDictionary&lt;Type, Action&lt;object&gt;&gt; cache;
  
  public static DoSomething(object o)
  {
      Action&lt;object&gt; action;
      if(cache.TryGetValue(o.GetType(), out action)) //Simple lookup, fast!
      {
          action(o);
      }
      else
      {
          // Do reflection to get the action
          // slow
      }
  } 
  </code></pre>
  
  <p>This leaks a bit of memory, but since it does that only once per Type and types lived as long as the <code>AppDomain</code> I didn't consider that a problem.</p>
  
  <h2>Since .net 4</h2>
  
  <p>But now .net 4 introduced <a href="http://msdn.microsoft.com/en-us/library/dd554932(VS.100).aspx" rel="noreferrer">Collectible Assemblies for Dynamic Type Generation</a>. If I ever used <code>DoSomething</code> on an object declared in the collectible assembly that assembly won't ever get unloaded. Ouch.</p>
  
  <p>So what's the best way to cache per type information in .net 4 that doesn't suffer from this problem? The easiest solution I can think of is a:</p>
  
  <pre><code>private static ConcurrentDictionary&lt;WeakReference, TCachedData&gt; cache.
  </code></pre>
  
  <p>But the <code>IEqualityComparer&lt;T&gt;</code> I'd have to use with that would behave very strangely and would probably violate the contract too. I'm not sure how fast the lookup would be either.</p>
  
  <p>Another idea is to use an expiration timeout. Might be the simplest solution, but feels a bit inelegant.</p>
  
  <hr>
  
  <p>In the cases where the type is supplied as generic parameter I can use a nested generic class which should not suffer from this problem. But his doesn't work if the type is supplied in a variable.</p>
  
  <pre><code>class MyReflection
  {
      internal Cache&lt;T&gt;
      {
          internal static TData data;
      }
  
      void DoSomething&lt;T&gt;()
      {
          DoSomethingWithData(Cache&lt;T&gt;.data);
          //Obviously simplified, should have similar creation logic to the previous code.
      }
  }
  </code></pre>
  
  <hr>
  
  <p><strong>Update</strong>: One idea I've just had is using <code>Type.AssemblyQualifiedName</code> as the key. That should uniquely identify that type without keeping it in memory. I might even get away with using referential identity on this string.</p>
  
  <p>One problem that remains with this solution is that the cached value might keep a reference to the type too. And if I use a weak reference for that it will most likely expire far before the assembly gets unloaded. And I'm not sure how cheap it is to Get a normal reference out of a weak reference. Looks like I need to do some testing and benchmarking.</p>