List all classes from a given namespace

1

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 Collection FindAllnamespaceClasses(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.

Technorati Tags:

Digg This
Reddit This
Stumble Now!
Buzz This
Vote on DZone
Share on Facebook
Bookmark this on Delicious
Kick It on DotNetKicks.com
Shout it
Share on LinkedIn
Bookmark this on Technorati
Post on Twitter
Google Buzz (aka. Google Reader)
Page 4 of 4« Previous1234

Note: Silverlight, C#, in fact any .NET web development projects is best used with windows hosting than Linux based hosting.