Skip to content

Instantly share code, notes, and snippets.

@JakubNei
Last active June 10, 2017 06:48
Show Gist options
  • Save JakubNei/2a62c9ffebe23cd82473 to your computer and use it in GitHub Desktop.
Save JakubNei/2a62c9ffebe23cd82473 to your computer and use it in GitHub Desktop.
Simple class to gather and iterate over all namespaces in all AppDomain.CurrentDomain.GetAssemblies(). It builds a tree of NamespaceNode. To generate it and to get root use NamespaceNode.Root.
/*
Simple class to gather and iterate over all namespaces in all AppDomain.CurrentDomain.GetAssemblies().
It builds a tree of NamespaceNode.
To generate it and to get root use NamespaceNode.Root.
license: WTFPL (http://www.wtfpl.net/)
author: aeroson
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Collections;
public class NamespaceNode : IEnumerable<NamespaceNode>
{
static IEnumerable<string> AllNamespaces
{
get
{
return AppDomain.CurrentDomain.GetAssemblies()
.SelectMany(assembly => assembly.GetTypes())
.Where(type => !string.IsNullOrEmpty(type.Namespace))
.Select(type => type.Namespace)
.Distinct();
}
}
public NamespaceNode Parent { get; private set; }
public string Name { get; private set; }
string m_FullName;
public string FullName
{
get
{
if (string.IsNullOrEmpty(m_FullName))
{
if (Parent != null)
{
if (!string.IsNullOrEmpty(Parent.FullName)) m_FullName = Parent.FullName + "." + Name;
else m_FullName = Name;
}
}
return m_FullName;
}
}
static NamespaceNode m_Root;
public static NamespaceNode Root
{
get
{
if (m_Root == null)
{
m_Root = new NamespaceNode("", null);
m_Root.Fill(AllNamespaces);
}
return m_Root;
}
}
Dictionary<string, NamespaceNode> childs = new Dictionary<string, NamespaceNode>();
public NamespaceNode(string name, NamespaceNode parent)
{
this.Name = name;
this.Parent = parent;
}
public bool HasChilds()
{
return childs.Count > 0;
}
public NamespaceNode GetChildFromFullNameSpace(string nameSpace, bool createIfDoesntExist = true)
{
var ns_parts = nameSpace.Split('.');
var child = this;
foreach (var ns_part in ns_parts)
{
child = child.GetChildFromNameSpacePart(ns_part, createIfDoesntExist);
}
return child;
}
public NamespaceNode GetChildFromNameSpacePart(string nameSpacePart, bool createIfDoesntExist = true)
{
NamespaceNode child;
if (!childs.TryGetValue(nameSpacePart, out child))
{
child = new NamespaceNode(nameSpacePart, this);
childs[nameSpacePart] = child;
}
return child;
}
public void Fill(IEnumerable<string> fulleNameSpaces)
{
foreach (var ns in fulleNameSpaces)
{
var ns_parts = ns.Split('.');
var child = this;
foreach (var ns_part in ns_parts)
{
child = child.GetChildFromNameSpacePart(ns_part, true);
}
}
}
public static implicit operator string (NamespaceNode me)
{
return me.FullName;
}
public override string ToString()
{
return FullName;
}
public IEnumerable<NamespaceNode> GetChilds()
{
return childs.Values;
}
public IEnumerator<NamespaceNode> GetEnumerator()
{
return GetChilds().GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetChilds().GetEnumerator();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment