title: ReflectionTypeLoadException, DllImport and Generic class
body: <p>I try use MEF in my project, but some cases I get this error:</p>
  
  <pre><code>System.Reflection.ReflectionTypeLoadException
  </code></pre>
  
  <p>Unable to load one or more of the requested types. Retrieve the LoaderExceptions property for more information.</p>
  
  <p>Generic method or method in generic class is internal call, PInvoke, or is defined in a COM Import class.</p>
  
  <p>In log exception it appears a generic class that use DllImport attrib.</p>
  
  <p>is it not possible to call pinvoke methods in a generic class ?</p>
  
  <p>I am still interested why this causes a problem.</p>
  
  <p>source code:</p>
  
  <pre><code>void Compose()
          {
              try
              {
                  DirectoryCatalog catalog =
                      new DirectoryCatalog(AppDomain.CurrentDomain.BaseDirectory);
  
                  AggregateCatalog agcatalogue = new AggregateCatalog(new ComposablePartCatalog[] {catalog, 
                      new AssemblyCatalog(Assembly.GetExecutingAssembly())});
  
                  CompositionContainer container = new CompositionContainer(agcatalogue);
  
                  CompositionBatch batch = new CompositionBatch();
  
                  batch.AddPart(this);
  
                  container.Compose(batch);
              }
              catch (FileNotFoundException fnfex)
              {
                  Console.WriteLine(fnfex.Message);
              }
              catch (CompositionException cex)
              {
                  Console.WriteLine(cex.Message);
              }
              catch (System.Reflection.ReflectionTypeLoadException rte)
              {
                  Console.WriteLine(rte.Message);
                  foreach (Exception ex in rte.LoaderExceptions)
                  {
                      Console.WriteLine(ex.ToString());
                  }
              }
              catch (Exception ex)
              {
                  Console.WriteLine(ex.Message);
              }
          }
  
  
  //Generic Class
   [Serializable]
      public class ProcessCounters&lt;T&gt; : IDisposable where T : ICounterData
      {
  
          /// &lt;summary&gt;
          /// Extern
          /// &lt;/summary&gt;
          /// &lt;param name="ticks"&gt;&lt;/param&gt;
          [DllImport("Kernel32.dll")]
          public static extern void QueryPerformanceCounter(ref long ticks);
  
          private static T _counterData;
  
  ...}
  </code></pre>
  
  <p><strong>Update:</strong>
  Limitation: You can't have DllImports in a generic class.  Put your DllImports in a 
  separate, static, class (as recommended).
  Solution: Solved it by moving the NativeMethods class outside of the generic class.</p>
  
  <p><a href="http://blog.appelgren.org/2006/05/04/nested-class-with-pinvoke-in-generic-class/" rel="nofollow">http://blog.appelgren.org/2006/05/04/nested-class-with-pinvoke-in-generic-class/</a></p>
  
  <p><a href="http://www.archivum.info/microsoft.public.dotnet.framework.clr/2006-12/00015/RE-TypeLoadException-when-instantiating-generic-class-that-uses-D.html" rel="nofollow">http://www.archivum.info/microsoft.public.dotnet.framework.clr/2006-12/00015/RE-TypeLoadException-when-instantiating-generic-class-that-uses-D.html</a></p>