How do you get all the classes in a namespace through reflection in C#?
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 :-) Jul 10, 2014 at 5:38 -
@Netsi1964 if you remove
&& t.Namespace == @namespace
you get all classes of all assemblies, including .net's.GetAssemblies
will give you all assemblies, andGetAssemblies().SelectMany(t => t.GetTypes())
will give all types (classes, structs etc) from all assemblies.– nawfalJul 10, 2014 at 5: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?– harvzorAug 26, 2019 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.– harvzorAug 26, 2019 at 13:17
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);
}
-
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.– GishuSep 17, 2008 at 3:41
-
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" Sep 19, 2008 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. Sep 19, 2008 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 theasm.GetTypes()
result.– ProfKOct 26, 2014 at 6:30 -
2Having 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.– JuhoNov 6, 2018 at 12:00
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
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!
-
Sure looks helpful, and less more helpful and less confusing than Ryan Farley's code even without thinking about it.– ProfKOct 26, 2014 at 6: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 fora
in helping withLoaderException
errors. Am I right?– ProfKOct 26, 2014 at 6: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.– nawfalMay 12, 2013 at 4:20 -
...and then loop through them, filtering out ones that don't match the namespace. Jun 29, 2015 at 7: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, which enumerates classes only, from all assemblies. Apr 14, 2020 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));
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();
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()
//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
-
What are
AttributeClass
the nameMustHaveAttributes
all about? I see nothing relating to testing whether a class has attributes or not. This is more confusing than helpful.– ProfKOct 26, 2014 at 6:43
Quite simple
Type[] types = Assembly.Load(new AssemblyName("mynamespace.folder")).GetTypes();
foreach (var item in types)
{
}
-
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.– IanMar 4, 2020 at 12:33