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
No, there’s no shortcut for this, although LINQ makes it relatively easy. For example, in C# the raw “set of namespaces” would be:
To get the top-level namespace instead you should probably write a method:
I’m intrigued as to why you only need top level namespaces though… it seems an odd constraint.