C # getting all namespaces from .dll

In a DLL (GUI) I need to get a list of all namespaces in the current application (for example, if I linked this DLL in a project with the name "Hundekuchen", it should List

{ "GUI", "Hundekuchen" }

Is there a built-in function for this?

+3
source share
3 answers

Take a look at the namespace System.Reflection, here you will find the functionality to get information about assemblies.

This may not be exactly what you need, but it shows what you can achieve, I get all the types in the assembly currently in progress, iterate over and print out the namespace of each type.

foreach (Type t in Assembly.GetExecutingAssembly().GetTypes())
{
    Console.WriteLine(t.Namespace);
}

Just look through the class Assemblyand you should find a way to solve your problem.

+1

, .

        Assembly asm = typeof(MyApplication).Assembly;
        List<string> namespaces = new List<string>();
        foreach (var type in asm.GetTypes())
        {
            string ns = type.Namespace;
            if (!namespaces.Contains(ns))
            {
                namespaces.Add(ns);
            }
        }

        foreach (var ns in namespaces)
        {
            Console.WriteLine(ns);
        }
+10

You can get a list of all the assemblies in AppDomain, and then all the classes in all loaded assemblers, but you will have to filter out the ones you do not want to see, because there are many assemblies related to mscorlib and the System Libraries. Here's a quick and dirty one (using .NET 3.5) - you may need to filter out a few more if you link other libraries that you don't want to see:

    var enumerable = AppDomain.CurrentDomain.GetAssemblies()
        .SelectMany(assembly => assembly.GetTypes())
        .Select(type => type.Namespace)
        .Distinct()
        .Where(name => name != null &&
                       !name.StartsWith("System") &&
                       !name.StartsWith("Microsoft") &&
                       !name.StartsWith("MS.") &&
                       !name.StartsWith("<"));
+6
source

All Articles