How can I get all classes within a namespace in C#?
Asked
Active
Viewed 1.1e+01k times
95
-
1possible duplicate of [Taking out all classes of a specific namespace](http://stackoverflow.com/questions/343869/taking-out-all-classes-of-a-specific-namespace) – nawfal Feb 25 '13 at 10:34
-
2Possible duplicate of [Getting all types in a namespace via reflection](https://stackoverflow.com/questions/79693/getting-all-types-in-a-namespace-via-reflection) – Liam Aug 29 '17 at 12:56
3 Answers
165
You will need to do it "backwards"; list all the types in an assembly and then checking the namespace of each type:
using System.Reflection;
private Type[] GetTypesInNamespace(Assembly assembly, string nameSpace)
{
return
assembly.GetTypes()
.Where(t => String.Equals(t.Namespace, nameSpace, StringComparison.Ordinal))
.ToArray();
}
Example of usage:
Type[] typelist = GetTypesInNamespace(Assembly.GetExecutingAssembly(), "MyNamespace");
for (int i = 0; i < typelist.Length; i++)
{
Console.WriteLine(typelist[i].Name);
}
For anything before .Net 2.0 where Assembly.GetExecutingAssembly()
is not available, you will need a small workaround to get the assembly:
Assembly myAssembly = typeof(<Namespace>.<someClass>).GetTypeInfo().Assembly;
Type[] typelist = GetTypesInNamespace(myAssembly, "<Namespace>");
for (int i = 0; i < typelist.Length; i++)
{
Console.WriteLine(typelist[i].Name);
}

ΩmegaMan
- 29,542
- 12
- 100
- 122

Fredrik Mörk
- 155,851
- 29
- 291
- 343
-
2.Where(t => String.Equals(t.Namespace, nameSpace, StringComparison.Ordinal) – abatishchev Jun 04 '09 at 08:40
-
9Also, keep in mind that Assembly != namespace - some namespaces are spread across multiple assemblies. – Bevan Jun 04 '09 at 08:41
-
1and why not to return just a IEnumerable
? All the more, you do an enumeration between the results, also 'foreach' instead of 'for' is better, I think. – abatishchev Jun 04 '09 at 08:44 -
2Good comments, thanks for that. By making the return type to an IEnumerable
eliminates the need for the last ToArray call. Not sure if I agree that 'foreach' is better than 'for' though; as I see it the performance difference is neglectible so I think it comes down to personal style. But may have a good argument for preferring 'foreach'; if so feel free to share it; I like to be proven wrong :) – Fredrik Mörk Jun 04 '09 at 08:55 -
4I think foreach is just easier to look at personally. Behind the scenes the two loops are pretty much the same in regards to performance. – CatDadCode Jun 08 '11 at 16:59
-
4
You'll need to provide a little more information...
Do you mean by using Reflection. You can iterate through an assemblies Manifest and get a list of types using
System.Reflection.Assembly myAssembly = Assembly.LoadFile("");
myAssembly.ManifestModule.FindTypes()
If it's just in Visual Studio, you can just get the list in the intellisense window, or by opening the Object Browser (CTRL+W, J)

Eoin Campbell
- 43,500
- 17
- 101
- 157
0
With Reflection you cal loop through all the types in an assembly. A type has a Namespace property which you use to filter only the namespace you're interested in.

Gerrie Schenck
- 22,148
- 20
- 68
- 95