c# - How to get all namespaces inside a namespace recursively -
to put simple, want namespaces in project recursively, , classes available in namespaces found earlier.
var namespaces = assembly.gettypes() .select(ns => ns.namespace);
i using part earlier namespaces in string format. got know underlying namespaces well.
it sounds might want lookup
namespace class:
var lookup = assembly.gettypes().tolookup(t => t.namespace);
or alternatively (and similarly) use groupby
:
var groups = assembly.gettypes().groupby(t => t.namespace);
for example:
var groups = assembly.gettypes() .where(t => t.isclass) // include classes .groupby(t => t.namespace); foreach (var group in groups) { console.writeline("namespace: {0}", group.key); foreach (var type in group) { console.writeline(" {0}", t.name); } }
however, it's not entirely clear whether that's you're after. classes in each namespace, don't know whether that's you're looking for.
two points bear in mind:
- there's nothing recursive this
- namespaces don't really form hierarchy, far clr concerned. there's no such thing "underlying" namespace. c# language does have rules this, far clr concerned, there's no such thing "parent" namespace.
if really want go "foo.bar.baz" "foo.bar" , "foo" can use like:
while (true) { console.writeline(ns); int index = ns.lastindexof('.'); if (index == -1) { break; } ns = ns.substring(0, index); }
Comments
Post a Comment