Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Finding all Namespaces in an assembly using Reflection (DotNET)

Tags:

I've got an assembly (loaded as ReflectionOnly) and I want to find all the namespaces in this assembly so I can convert them into "using" ("Imports" in VB) statements for an auto-generated source code file template.

Ideally I'd like to restrict myself to top-level namespaces only, so instead of:

using System;
using System.Collections;
using System.Collections.Generic;

you'd only get:

using System;

I noticed there is a Namespace property on the System.Type class, but is there a better way to collect Namespaces inside an assembly that doesn't involve iterating over all types and culling duplicate namespace strings?

Much obliged, David

like image 525
David Rutten Avatar asked Oct 10 '09 22:10

David Rutten


People also ask

Which namespace helps to find information of an assembly?

Reflection namespace, together with System. Type, enable you to obtain information about loaded assemblies and the types defined within them, such as classes, interfaces, and value types (that is, structures and enumerations).

Which namespace helps to find information of an assembly in C#?

Assembly. GetTypes(); This will get you a collection of all types and then you can get the Namespace property for each of them.

Can assembly have multiple namespaces?

Assemblies can contain multiple namespaces. Namespaces can contain other namespaces. An assembly provides a fundamental unit of physical code grouping. A namespace provides a fundamental unit of logical code grouping.

How do I find the DLL namespace?

If you use Visual Studio's object browser (Ctrl+Alt+J), you can view all the referenced types, either by namespace or reference. If you view by namespace, select a type within a namespace, then view by container, it'll bring you to the reference which contains it.


2 Answers

No, there's no shortcut for this, although LINQ makes it relatively easy. For example, in C# the raw "set of namespaces" would be:

var namespaces = assembly.GetTypes()
                         .Select(t => t.Namespace)
                         .Distinct();

To get the top-level namespace instead you should probably write a method:

var topLevel = assembly.GetTypes()
                       .Select(t => GetTopLevelNamespace(t))
                       .Distinct();

...

static string GetTopLevelNamespace(Type t)
{
    string ns = t.Namespace ?? "";
    int firstDot = ns.IndexOf('.');
    return firstDot == -1 ? ns : ns.Substring(0, firstDot);
}

I'm intrigued as to why you only need top level namespaces though... it seems an odd constraint.

like image 193
Jon Skeet Avatar answered Oct 14 '22 18:10

Jon Skeet


Namespaces are really just a naming convention in type names, so they only "exist" as a pattern that is repeated across many qualified type names. So you have to loop through all the types. However, the code for this can probably be written as a single Linq expression.

like image 33
Daniel Earwicker Avatar answered Oct 14 '22 16:10

Daniel Earwicker