title: Reflection and dynamic in-memory assemblies
body: <p>Let us say I create a type dynamically using CSharpCodeProvider and choose NOT to persist the results. The assembly that is generated is existing only in memory.
  Let us say I Create two types in two different in-memory assemblies:</p>
  
  <p>Assembly1:</p>
  
  <pre><code>public class DynamicTypeA { }
  </code></pre>
  
  <p>Assembly2:</p>
  
  <pre><code>public class DynamicTypeB
  {
         public DynamicTypeA MyProperty { get; set; }
  }
  </code></pre>
  
  <p>As you can see the second type has a property of the first type.
  Cool. Now I want to explore DynamicTypeB using reflection:</p>
  
  <pre><code>foreach (PropertyInfo pi in typeof(DynamicTypeB).GetProperties())
  {
      Console.WriteLine(pi.PropertyType.Name);
  }
  </code></pre>
  
  <p>It turns out that PropertyInfo.PropertyType fails when the assembly is not located on disk !!!
  This is true for MemberInfo and for all other type investigation constructs.</p>
  
  <p>As we all know lots of .Net APIs is using type investigation on the backend and they would fail when the investigated type happens to live in an in-memory assembly. For Example Expression.Bind takes a MemberInfo as the first parameter and is using it to validate that the type of the expression provided in the second parameter matches the type of the member. When this type happens to be in an in memory assembly Expression.Bind fails.</p>
  
  <p>Can anyone think of a solution? </p>
  
  <p>Creating types dynamically and writing them do disk pollutes the running environment and that is bad, yet without reflection these types are worthless.</p>
  
  <p>Thanks
  Manu</p>