<p>How do you get all the classes in a namespace through reflection in C#?</p>

[{'answer_id': 79706, 'author': 'Ryan Farley', 'author_id': 1627, 'author_profile': 'https://Stackoverflow.com/users/1627', 'pm_score': 5, 'selected': False, 'text': "<pre><code>using System.Reflection;\nusing System.Collections.Generic;\n//...\n\nstatic List&lt;string&gt; GetClasses(string nameSpace)\n{\n    Assembly asm = Assembly.GetExecutingAssembly();\n\n    List&lt;string&gt; namespacelist = new List&lt;string&gt;();\n    List&lt;string&gt; classlist = new List&lt;string&gt;();\n\n    foreach (Type type in asm.GetTypes())\n    {\n        if (type.Namespace == nameSpace)\n            namespacelist.Add(type.Name);\n    }\n\n    foreach (string classname in namespacelist)\n        classlist.Add(classname);\n\n    return classlist;\n}\n</code></pre>\n\n<p>NB: The above code illustrates what's going on.  Were you to implement it, a simplified version can be used:</p>\n\n<pre><code>using System.Linq;\nusing System.Reflection;\nusing System.Collections.Generic;\n//...\n\nstatic IEnumerable&lt;string&gt; GetClasses(string nameSpace)\n{\n    Assembly asm = Assembly.GetExecutingAssembly();\n    return asm.GetTypes()\n        .Where(type =&gt; type.Namespace == nameSpace)\n        .Select(type =&gt; type.Name);\n}\n</code></pre>\n"}, {'answer_id': 79712, 'author': 'FlySwat', 'author_id': 1965, 'author_profile': 'https://Stackoverflow.com/users/1965', 'pm_score': 3, 'selected': False, 'text': "<p>You won't be able to get all types in a namespace, because a namespace can bridge multiple assemblies, but you can get all classes in an assembly and check to see if they belong to that namespace.</p>\n\n<p><code>Assembly.GetTypes()</code> works on the local assembly, or you can load an assembly first then call <code>GetTypes()</code> on it.</p>\n"}, {'answer_id': 79738, 'author': 'aku', 'author_id': 1196, 'author_profile': 'https://Stackoverflow.com/users/1196', 'pm_score': 8, 'selected': False, 'text': '<p>Following code prints names of classes in specified <code>namespace</code> defined in current assembly.<br>\nAs other guys pointed out, a namespace can be scattered between different modules, so you need to get a list of assemblies first.</p>\n\n<pre><code>string nspace = "...";\n\nvar q = from t in Assembly.GetExecutingAssembly().GetTypes()\n        where t.IsClass &amp;&amp; t.Namespace == nspace\n        select t;\nq.ToList().ForEach(t =&gt; Console.WriteLine(t.Name));\n</code></pre>\n'}, {'answer_id': 79785, 'author': 'TheXenocide', 'author_id': 8543, 'author_profile': 'https://Stackoverflow.com/users/8543', 'pm_score': 2, 'selected': False, 'text': '<p>Namespaces are actually rather passive in the design of the runtime and serve primarily as organizational tools. The Full Name of a type in .NET consists of the Namespace and Class/Enum/Etc. combined. If you only wish to go through a specific assembly, you would simply loop through the types returned by assembly.<a href="http://msdn.microsoft.com/en-us/library/system.reflection.assembly.getexportedtypes.aspx" rel="nofollow noreferrer">GetExportedTypes()</a> checking the value of type.<a href="http://msdn.microsoft.com/en-us/library/system.type.namespace.aspx" rel="nofollow noreferrer">Namespace</a>. If you were trying to go through all assemblies loaded in the current AppDomain it would involve using AppDomain.CurrentDomain.<a href="http://msdn.microsoft.com/en-us/library/system.appdomain_members.aspx" rel="nofollow noreferrer">GetAssemblies()</a></p>\n'}, {'answer_id': 79793, 'author': 'tsimon', 'author_id': 1685, 'author_profile': 'https://Stackoverflow.com/users/1685', 'pm_score': 4, 'selected': False, 'text': "<p>Here's a fix for LoaderException errors you're likely to find if one of the types sublasses a type in another assembly:</p>\n\n<pre><code>// Setup event handler to resolve assemblies\nAppDomain.CurrentDomain.ReflectionOnlyAssemblyResolve += new ResolveEventHandler(CurrentDomain_ReflectionOnlyAssemblyResolve);\n\nAssembly a = System.Reflection.Assembly.ReflectionOnlyLoadFrom(filename);\na.GetTypes();\n// process types here\n\n// method later in the class:\nstatic Assembly CurrentDomain_ReflectionOnlyAssemblyResolve(object sender, ResolveEventArgs args)\n{\n    return System.Reflection.Assembly.ReflectionOnlyLoad(args.Name);\n}\n</code></pre>\n\n<p>That should help with loading types defined in other assemblies.</p>\n\n<p>Hope that helps!</p>\n"}, {'answer_id': 762978, 'author': 'Yordan Georgiev', 'author_id': 65706, 'author_profile': 'https://Stackoverflow.com/users/65706', 'pm_score': 2, 'selected': False, 'text': '<pre><code>//a simple combined code snippet \n\nusing System;\nusing System.Collections.Generic;\nusing System.Linq;\nusing System.Text;\nusing System.Reflection;\n\nnamespace MustHaveAttributes\n{\n  class Program\n  {\n    static void Main ( string[] args )\n    {\n      Console.WriteLine ( " START " );\n\n      // what is in the assembly\n      Assembly a = Assembly.Load ( "MustHaveAttributes" );\n      Type[] types = a.GetTypes ();\n      foreach (Type t in types)\n      {\n\n        Console.WriteLine ( "Type is {0}", t );\n      }\n      Console.WriteLine (\n         "{0} types found", types.Length );\n\n      #region Linq\n      //#region Action\n\n\n      //string @namespace = "MustHaveAttributes";\n\n      //var q = from t in Assembly.GetExecutingAssembly ().GetTypes ()\n      //        where t.IsClass &amp;&amp; t.Namespace == @namespace\n      //        select t;\n      //q.ToList ().ForEach ( t =&gt; Console.WriteLine ( t.Name ) );\n\n\n      //#endregion Action  \n      #endregion\n\n      Console.ReadLine ();\n      Console.WriteLine ( " HIT A KEY TO EXIT " );\n      Console.WriteLine ( " END " );\n    }\n  } //eof Program\n\n\n  class ClassOne\n  {\n\n  } //eof class \n\n  class ClassTwo\n  {\n\n  } //eof class\n\n\n  [System.AttributeUsage ( System.AttributeTargets.Class |\n    System.AttributeTargets.Struct, AllowMultiple = true )]\n  public class AttributeClass : System.Attribute\n  {\n\n    public string MustHaveDescription { get; set; }\n    public string MusHaveVersion { get; set; }\n\n\n    public AttributeClass ( string mustHaveDescription, string mustHaveVersion )\n    {\n      MustHaveDescription = mustHaveDescription;\n      MusHaveVersion = mustHaveVersion;\n    }\n\n  } //eof class \n\n} //eof namespace \n</code></pre>\n'}, {'answer_id': 14234375, 'author': 'JoanComasFdz', 'author_id': 383129, 'author_profile': 'https://Stackoverflow.com/users/383129', 'pm_score': 3, 'selected': False, 'text': '<p>Just like @aku answer, but using extension methods:</p>\n\n<pre><code>string @namespace = "...";\n\nvar types = Assembly.GetExecutingAssembly().GetTypes()\n    .Where(t =&gt; t.IsClass &amp;&amp; t.Namespace == @namespace)\n    .ToList();\n\ntypes.ForEach(t =&gt; Console.WriteLine(t.Name));\n</code></pre>\n'}, {'answer_id': 16504427, 'author': 'nawfal', 'author_id': 661933, 'author_profile': 'https://Stackoverflow.com/users/661933', 'pm_score': 7, 'selected': False, 'text': '<p>As FlySwat says, you can have the same namespace spanning in multiple assemblies (for eg <code>System.Collections.Generic</code>). You will have to load all those assemblies if they are not already loaded. So for a complete answer:</p>\n\n<pre><code>AppDomain.CurrentDomain.GetAssemblies()\n                       .SelectMany(t =&gt; t.GetTypes())\n                       .Where(t =&gt; t.IsClass &amp;&amp; t.Namespace == @namespace)\n</code></pre>\n\n<p>This should work unless you want classes of other domains. To get a list of all domains, follow <a href="https://stackoverflow.com/questions/388554/list-appdomains-in-process">this link.</a></p>\n'}, {'answer_id': 27598813, 'author': 'Ivo Stoyanov', 'author_id': 2298241, 'author_profile': 'https://Stackoverflow.com/users/2298241', 'pm_score': 3, 'selected': False, 'text': '<p>Get all classes by part of Namespace name in just one row:</p>\n\n<pre><code>var allClasses = Assembly.GetExecutingAssembly().GetTypes().Where(a =&gt; a.IsClass &amp;&amp; a.Namespace != null &amp;&amp; a.Namespace.Contains(@"..your namespace...")).ToList();\n</code></pre>\n'}, {'answer_id': 34869091, 'author': 'JWP', 'author_id': 1522548, 'author_profile': 'https://Stackoverflow.com/users/1522548', 'pm_score': 5, 'selected': False, 'text': '<p>For a specific Assembly, NameSpace and ClassName:</p>\n\n<pre><code>var assemblyName = "Some.Assembly.Name"\nvar nameSpace = "Some.Namespace.Name";\nvar className = "ClassNameFilter";\n\nvar asm = Assembly.Load(assemblyName);\nvar classes = asm.GetTypes().Where(p =&gt;\n     p.Namespace == nameSpace &amp;&amp;\n     p.Name.Contains(className) \n).ToList();\n</code></pre>\n\n<p>Note: The project must reference the assembly</p>\n'}, {'answer_id': 35707879, 'author': 'Antonio Lopes', 'author_id': 4837664, 'author_profile': 'https://Stackoverflow.com/users/4837664', 'pm_score': 0, 'selected': False, 'text': '<p>Quite simple</p>\n\n<pre><code>Type[] types = Assembly.Load(new AssemblyName("mynamespace.folder")).GetTypes();\nforeach (var item in types)\n{\n}\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/79693', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/-1/']