June 14, 2008
This is a little snipplet i’ve been using over the years in several tasks and it’s purpose is to get a listing of all class names that belong to a given namespace. For this we’ll be using the System.Reflection namespace:
public CollectionFindAllnamespaceClasses(string namespaceName) { Assembly currentAssembly = Assembly.GetExecutingAssembly(); // Set list to hold all namespaces List namespaceList = new List (); // Set list to hold all namespace classes Collection classList = new Collection (); foreach (Type type in currentAssembly.GetTypes()) { if (type.Namespace.ToUpper().Contains(namespaceName)) namespaceList.Add(type.Name); } // Loop through all the classes and add them foreach (string className in namespaceList) classList.Add(className); return classList; }
And that’s it. This will return all the classes that belong to the specified namespace. If you need to infer over namespaces belonging on a different assembly, line 3 should use the Assembly.GetAssembly() method, and pass a type belonging to that same namespace as parameter.












