title: How to make C# recognize custom attributes in external .dll?
body: <p>The code below successfully recognizes <em>internal</em> classes which are decorated with my custom "Module" attribute, I load the assembly like this:</p>
  
  <pre><code>Assembly assembly = Assembly.GetExecutingAssembly();
  </code></pre>
  
  <p>However, when I load in an <strong>external module</strong> and look through its classes, it finds the classes in the external assembly but <strong>does not recognize the custom attributes</strong>:</p>
  
  <pre><code>Assembly assembly = Assembly.LoadFrom(@"c:\tests\modules\CustomModules.dll");
  </code></pre>
  
  <p><strong>What do I have to specify so that C# recognizes custom attributes in external .dlls the same as it does with the internal classes?</strong></p>
  
  <p>Here is the code that successfully runs through and recognizes internal classes decorated with my "Module" attribute:</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Reflection;
  
  namespace DynamicAssembly2
  {
      class Program
      {
          static void Main(string[] args)
          {
              var modules = from t in GetModules()
                             select t;
              foreach (var module in modules)
              {
                  ModuleAttribute[] moduleAttributes = GetModuleAttributes(module);
                  Console.WriteLine(module.FullName);
                  foreach (var moduleAttribute in moduleAttributes)
                  {
                      Console.WriteLine(moduleAttribute.Description);
                  }
              }
              Console.ReadLine();
          }
  
          public static IEnumerable&lt;Type&gt; GetModules()
          {
              //Assembly assembly = Assembly.LoadFrom(@"c:\tests\modules\CustomModules.dll");
              Assembly assembly = Assembly.GetExecutingAssembly();
  
              return GetAssemblyClasses(assembly)
                  .Where((Type type) =&gt; {return IsAModule(type);});
          }
  
          public static IEnumerable&lt;Type&gt; GetAssemblyClasses(Assembly assembly)
          {
              foreach (Type type in assembly.GetTypes())
              {
                  Console.WriteLine(type.FullName);
                  yield return type;
              }
          }
  
          public static bool IsAModule(Type type)
          {
              return GetModuleAttribute(type) != null;
          }
  
          public static ModuleAttribute GetModuleAttribute(Type type)
          {
              ModuleAttribute[] moduleAttributes = GetModuleAttributes(type);
              Console.WriteLine(moduleAttributes.Length);
              if (moduleAttributes != null &amp;&amp; moduleAttributes.Length != 0)
                  return moduleAttributes[0];
              return null;
          }
  
          public static ModuleAttribute[] GetModuleAttributes(Type type)
          {
              return (ModuleAttribute[])type.GetCustomAttributes(typeof(ModuleAttribute), true);
          }
  
      }
  }
  </code></pre>
  
  <p>Here is my custom Mdoule attribute:</p>
  
  <pre><code>using System;
  
  namespace DynamicAssembly2
  {
      [AttributeUsage(AttributeTargets.Class)]
      public class ModuleAttribute : Attribute
      {
          public string Description { get; set; }
      }
  }
  </code></pre>
  
  <p>Here is a custom module:</p>
  
  <pre><code>namespace DynamicAssembly2
  {
      [Module(Description="This is the main customer class.")]
      class Customers
      {
      }
  }
  </code></pre>