This web site uses cookies. By using the site you accept the cookie policy.This message is for compliance with the UK ICO law.

.NET 3.5+

Identifying the Namespaces in an Assembly

Sometimes it is necessary to find the names of all of the namespaces that are defined in a .NET assembly. Although reflection does not provide a method for this purpose, it is possible to get this information indirectly.

Reflecting Namespaces

If you need to retrieve a complete or partial list of the namespaces within an assembly, you will find that reflection doesn't provide this facility directly. It is possible to obtain the namespace within which a known type resides but there is no method within the Assembly class to obtain its namespaces.

In order to get the namespaces for an assembly you must use several reflection features. Firstly, you obtain an Assembly instance, either by getting the currently executing assembly or by loading an assembly using the static methods of the Assembly class. You can query this object to get a list of all of the types defined within using the GetTypes method.

Each of the Type objects that you retrieve using includes a property named, "Namespace". This returns a string containing the name of the namespace within which the type is defined. This might be a simple single-word name such as "System". For nested namespaces the string will include full stops (periods), for example, "System.Reflection".

Combining all of the types' Namespace properties into a collection gives a full list of namespaces in the assembly but this will likely include many duplicates. If you are using .NET 3.5 or later, these are easily removed using the Distinct operator provided by Language-Integrated Query (LINQ). For versions of .NET that do not include LINQ you would need to loop through the types, extracting the Namespace values into a new collection and ignoring duplicates.

We can combine this functionality into a simple method. Create a new console application and add the following using directive to the automatically created class:

using System.Reflection;

The method is shown below. Most of the work is done by the LINQ operators after calling GetTypes on the provided assembly. First the Type array is projected using Select to obtain a sequence of Namespace strings. Distinct removes the duplicated namespaces. The Where operator is included to remove nulls, which are present if any of the examined types has no namespace.

The method is static in this case so that you can call it from Main without creating an object first.

private static IEnumerable<string> GetNamespacesInAssembly(Assembly asm)
    Type[] types = asm.GetTypes();

    return types.Select(t => t.Namespace)
                .Where(n => n != null);

To try the code, add the following code to the Main method and run the program. This shows all of the namespaces defined in "mscorlib.dll".

Assembly asm = Assembly.Load("mscorlib.dll");
var namespaces = GetNamespacesInAssembly(asm);

foreach (string ns in namespaces.OrderBy(n => n))
22 April 2013