title: Loading an assembly and applying a predicate on its types in another appdomain
body: <p>Please read the entire question. I have a unique situation with several constraints that I'd like to solve.</p>
  
  <p>In my code I have an expression tree which is compiled to a <code>Predicate&lt;System.Type&gt;</code>. My goal is to load an assembly without locking it (it is the output assembly of the project, being rebuilt constantly), apply this predicate on the list of its types and get back a list of the resulting type <strong>names</strong>:</p>
  
  <pre><code>// this is what I need:
  return assembly.GetTypes().Where(t =&gt; predicate(t)).Select(t =&gt; t.FullName);
  </code></pre>
  
  <p>This assembly should be loaded in another appdomain, because I want to unload it as soon as I get the information I require.</p>
  
  <p>Here is where it gets tricky. There are several problems I'm facing:</p>
  
  <p>If I load the assembly in another appdomain and simply return an array of all of its types, so that I could apply the predicate back in my main appdomain, as soon as the Types are marshalled back to my main appdomain I get a <code>FileNotFoundException</code>, stating that the this assembly is not found. This makes sence, because the assembly is only being loaded in another appdomain I created. Loading it also in the main appdomain will defeat the purpose.</p>
  
  <p>If, alternatively, I try to pass the predicate into the other appdomain, to apply it there and get back an array of strings (full type name), I get a <code>SerializationException: "Cannot serialize delegates over unmanaged function pointers, dynamic methods or methods outside the delegate creator's assembly."</code>, because the predicate is a Dynamic Method (compiled from an expression tree).</p>
  
  <p>Loading it in the primary appdomain would have none of this problems, but since it's impossible to unload a loaded assembly without unloading the entire appdomain, as soon as the assembly would change (after rebuild), any attempt to load an assembly with the same name would result in an exception.</p>
  
  <p><strong>Context:</strong><br>
  I am building a plugin for ReSharper called <a href="https://github.com/hmemcpy/AgentMulder" rel="noreferrer">Agent Mulder</a>. The idea behind the plugin is to analyze DI/IoC Container registrations in your solution, and help ReSharper figure out the usage of types registered via a DI Container (you can watch a short video of how it works <a href="https://vimeo.com/41113265" rel="noreferrer">here</a>).</p>
  
  <p>For the most part, analyzing container registration is straightforward - I only have to detect enough information to know which concrete types are affected. In this example with Castle Windsor: <code>Component.For&lt;IFoo&gt;().ImplementedBy&lt;Foo&gt;()</code> the resulting type is obvious, so is <code>AllTypes.FromThisAssembly().BasedOn&lt;IFoo&gt;()</code> - giving me just enough information to guestimate the concrete types that will be affected by this line. However, consider this registration in Castle Windsor:</p>
  
  <pre><code>container.Register(Classes
      .FromAssemblyInDirectory(new AssemblyFilter(".").FilterByName(an =&gt; an.Name.StartsWith("Ploeh.Samples.Booking")))
      .Where(t =&gt; !(t.IsGenericType &amp;&amp; t.GetGenericTypeDefinition() == typeof(Dispatcher&lt;&gt;)))
      .WithServiceAllInterfaces());
  </code></pre>
  
  <p>(<a href="https://github.com/ploeh/Booking/blob/master/BookingDaemon/DaemonWindsorInstaller.cs" rel="noreferrer">source</a>)</p>
  
  <p>Here the information depends on a predicate that will only be evaluated at runtime.</p>
  
  <p>Since all I can do is statically analyze this, I have in my hand the ReSharper's AST (called PSI in ReSharper) representation of the lambda expression from the <code>Where</code> clause. I can convert this AST into a LINQ expression tree, then compile it into a delegate.</p>
  
  <p>My idea was is to load the output assembly (determined by the <code>FromAssembly*</code> descriptor) via reflection, and apply this delegate on the assembly's types in order to get the type names (I only need the names). This will have to be re-evaluated every time the assembly changes, too (I'm not concerned at this point about performance).</p>
  
  <p>In conclusion, unless someone could recommend a better way of determine the types affected by the predicate, I'd like to know how to do this with reflection (unfortunately I hadn't considered other metadata readers, because I'd have to somehow convert the lambda expression AST to a predicate of different data type, and I don't know if there exists a 1-to-1 conversion).</p>
  
  <p>Thank you for reading. This question will have a 500 point bounty when it becomes available.</p>