How do you get all the classes in a namespace through reflection in C#?
-
can you edit your question... the subtext question is a more communicative than the 'Namespace in C#' – Gishu Sep 17 '08 at 03:42
-
You can look [here](http://stackoverflow.com/questions/949246/how-to-get-all-classes-within-namespace). There are 2 different samples. – Fatih GÜRDAL Nov 15 '16 at 05:58
11 Answers
Following code prints names of classes in specified namespace
defined in current assembly.
As other guys pointed out, a namespace can be scattered between different modules, so you need to get a list of assemblies first.
string nspace = "...";
var q = from t in Assembly.GetExecutingAssembly().GetTypes()
where t.IsClass && t.Namespace == nspace
select t;
q.ToList().ForEach(t => Console.WriteLine(t.Name));
As FlySwat says, you can have the same namespace spanning in multiple assemblies (for eg System.Collections.Generic
). You will have to load all those assemblies if they are not already loaded. So for a complete answer:
AppDomain.CurrentDomain.GetAssemblies()
.SelectMany(t => t.GetTypes())
.Where(t => t.IsClass && t.Namespace == @namespace)
This should work unless you want classes of other domains. To get a list of all domains, follow this link.
-
1works fine - a small reminder: I tried to remove "`&& t.Namespace == @namespace`" - which ofcause gave me **all** .net assemblies :-) – Netsi1964 Jul 10 '14 at 05:38
-
@Netsi1964 if you remove `&& t.Namespace == @namespace` you get all *classes* of *all assemblies*, including .net's. `GetAssemblies` will give you all assemblies, and `GetAssemblies().SelectMany(t => t.GetTypes())` will give all types (classes, structs etc) from all assemblies. – nawfal Jul 10 '14 at 05:42
-
I upgraded to DotNet Core 2.2 (from 2.1) and this code stopped working for my specific assembly. The assembly I wanted was not referenced anywhere in the code so was not loaded! In 2.1 it was loaded, but 2.2 seems to have lazy loading? – harvzor Aug 26 '19 at 12:14
-
-
@nawfal Yeah. This code worked previously in 2.1. I found that I force the loading of an assembly by using `Assembly.Load(nameof(NameOfMyNamespace))` worked just fine. – harvzor Aug 26 '19 at 13:17
-
It's worth noting that the assembly I wanted loaded was not the current one! – harvzor Aug 26 '19 at 13:18
-
Thank you a lot. This one works in blazor code on the client side(webassembly). Super! – Ricardo Araújo Dec 26 '21 at 00:11
-
Credit to this answer this many years later. Works in .NET 6, too. In case it helps someone, I had to import the namespace (i.e., using ...) in my console application to get it to recognize the namespace string. – Tangere Apps Mar 19 '22 at 21:56
using System.Reflection;
using System.Collections.Generic;
//...
static List<string> GetClasses(string nameSpace)
{
Assembly asm = Assembly.GetExecutingAssembly();
List<string> namespacelist = new List<string>();
List<string> classlist = new List<string>();
foreach (Type type in asm.GetTypes())
{
if (type.Namespace == nameSpace)
namespacelist.Add(type.Name);
}
foreach (string classname in namespacelist)
classlist.Add(classname);
return classlist;
}
NB: The above code illustrates what's going on. Were you to implement it, a simplified version can be used:
using System.Linq;
using System.Reflection;
using System.Collections.Generic;
//...
static IEnumerable<string> GetClasses(string nameSpace)
{
Assembly asm = Assembly.GetExecutingAssembly();
return asm.GetTypes()
.Where(type => type.Namespace == nameSpace)
.Select(type => type.Name);
}

- 22,735
- 13
- 96
- 178

- 11,315
- 4
- 46
- 43
-
1Note: You can do this provided you input the assembly and the NS to look for. Types can be defined in multiple assemblies and belong to the same NS. – Gishu Sep 17 '08 at 03:41
-
That is true Gishu. I suppose it would be better to pass the assembly as well as namespace from the assembly you want to remove the ambiguity. – Ryan Farley Sep 17 '08 at 03:43
-
10I'm not trying to be mean, but there is an entirely unnecessary list and iteration through all of the found items in this code; the "classlist" variable and foreach through "namespacelist" provide no functionality different from returning "namespacelist" – TheXenocide Sep 19 '08 at 15:23
-
12@TheXenocide the purpose of a code sample is not always meant to show the "best" way to write code, but to clearly convey how something is done. – Ryan Farley Sep 19 '08 at 15:46
-
4The only you thing using two lists and two iterations helped was to slow me down trying to figure out just why you used two lists and didn't just add straight to `classlist` on the first iteration over the `asm.GetTypes()` result. – ProfK Oct 26 '14 at 06:30
-
3Having two lists here when you only have single list of data to work with is terrible code. It doesn't make anything clearer and encourages bad coding habits for beginners who read it. – Juho Nov 06 '18 at 12:00
-
To emphasize that the classlist was the same as the namespacelist, `var classlist = namespacelist; return classlist;` would have achieved the same purpose without the unnecessary loop distraction. – deleb Jun 17 '22 at 23:40
For a specific Assembly, NameSpace and ClassName:
var assemblyName = "Some.Assembly.Name"
var nameSpace = "Some.Namespace.Name";
var className = "ClassNameFilter";
var asm = Assembly.Load(assemblyName);
var classes = asm.GetTypes().Where(p =>
p.Namespace == nameSpace &&
p.Name.Contains(className)
).ToList();
Note: The project must reference the assembly

- 6,672
- 3
- 50
- 74
Here's a fix for LoaderException errors you're likely to find if one of the types sublasses a type in another assembly:
// Setup event handler to resolve assemblies
AppDomain.CurrentDomain.ReflectionOnlyAssemblyResolve += new ResolveEventHandler(CurrentDomain_ReflectionOnlyAssemblyResolve);
Assembly a = System.Reflection.Assembly.ReflectionOnlyLoadFrom(filename);
a.GetTypes();
// process types here
// method later in the class:
static Assembly CurrentDomain_ReflectionOnlyAssemblyResolve(object sender, ResolveEventArgs args)
{
return System.Reflection.Assembly.ReflectionOnlyLoad(args.Name);
}
That should help with loading types defined in other assemblies.
Hope that helps!

- 8,362
- 2
- 30
- 41
-
Sure looks helpful, and less more helpful and less confusing than Ryan Farley's code even without thinking about it. – ProfK Oct 26 '14 at 06:32
-
You did also have me confused for a while though. I still can only guess that the `Assembly a` stuff represents the normal processing that might cause this event to fire. I see no use for `a` in helping with `LoaderException` errors. Am I right? – ProfK Oct 26 '14 at 06:51
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.
Assembly.GetTypes()
works on the local assembly, or you can load an assembly first then call GetTypes()
on it.
-
2+1 for the correct answer. `AppDomain.CurrentDomain.GetAssemblies` can be helpful. – nawfal May 12 '13 at 04:20
-
...and then loop through them, filtering out ones that don't match the namespace. – T.J. Crowder Jun 29 '15 at 07:04
-
OP specifically asked for "classes in a namespace", whereas this gets you "types in an assembly" - so this answer is incomplete. The correct answer is probably [this one](https://stackoverflow.com/a/14234375/283851), which enumerates classes only, from all assemblies. – mindplay.dk Apr 14 '20 at 10:18
Just like @aku answer, but using extension methods:
string @namespace = "...";
var types = Assembly.GetExecutingAssembly().GetTypes()
.Where(t => t.IsClass && t.Namespace == @namespace)
.ToList();
types.ForEach(t => Console.WriteLine(t.Name));

- 2,911
- 5
- 34
- 50
Get all classes by part of Namespace name in just one row:
var allClasses = Assembly.GetExecutingAssembly().GetTypes().Where(a => a.IsClass && a.Namespace != null && a.Namespace.Contains(@"..your namespace...")).ToList();

- 16,256
- 8
- 62
- 65
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.GetExportedTypes() checking the value of type.Namespace. If you were trying to go through all assemblies loaded in the current AppDomain it would involve using AppDomain.CurrentDomain.GetAssemblies()

- 1,060
- 8
- 22
//a simple combined code snippet
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
namespace MustHaveAttributes
{
class Program
{
static void Main ( string[] args )
{
Console.WriteLine ( " START " );
// what is in the assembly
Assembly a = Assembly.Load ( "MustHaveAttributes" );
Type[] types = a.GetTypes ();
foreach (Type t in types)
{
Console.WriteLine ( "Type is {0}", t );
}
Console.WriteLine (
"{0} types found", types.Length );
#region Linq
//#region Action
//string @namespace = "MustHaveAttributes";
//var q = from t in Assembly.GetExecutingAssembly ().GetTypes ()
// where t.IsClass && t.Namespace == @namespace
// select t;
//q.ToList ().ForEach ( t => Console.WriteLine ( t.Name ) );
//#endregion Action
#endregion
Console.ReadLine ();
Console.WriteLine ( " HIT A KEY TO EXIT " );
Console.WriteLine ( " END " );
}
} //eof Program
class ClassOne
{
} //eof class
class ClassTwo
{
} //eof class
[System.AttributeUsage ( System.AttributeTargets.Class |
System.AttributeTargets.Struct, AllowMultiple = true )]
public class AttributeClass : System.Attribute
{
public string MustHaveDescription { get; set; }
public string MusHaveVersion { get; set; }
public AttributeClass ( string mustHaveDescription, string mustHaveVersion )
{
MustHaveDescription = mustHaveDescription;
MusHaveVersion = mustHaveVersion;
}
} //eof class
} //eof namespace

- 5,114
- 1
- 56
- 53
-
What are`AttributeClass` the name `MustHaveAttributes` all about? I see nothing relating to testing whether a class has attributes or not. This is more confusing than helpful. – ProfK Oct 26 '14 at 06:43
Quite simple
Type[] types = Assembly.Load(new AssemblyName("mynamespace.folder")).GetTypes();
foreach (var item in types)
{
}

- 17
- 1
-
1And quite simply not answering the question. All this is doing is getting a list of all types in a single assembly, irrespective of any particular namespace. – Ian Mar 04 '20 at 12:33