2009-02-13

Reflection - Scanning a Namespace for Classes.

When you're writing a lot of redundant code across several different classes, it's often a time to consider using Reflection to automate some of that using Reflection. If you've never used .NET's reflection toolset, you may wish to first head over to MSDN's reflection documentation.

http://msdn.microsoft.com/en-us/library/cxz4wk15.aspx

Reflection is a powerful set of tools that lets you scan appropriate the contents of assemblies, namespaces, and classes to understand all sorts of things--methods, properties, attributes, etc. There's a lot of power in reflection, and you can use it to automate some very mundane tasks--and more importantly, commonize redundant code.

I've recently found a great article [http://www.dreamincode.net/code/snippet1539.htm] that let's you scan a Namespace to identify all Classes, returned in the form a List<string>.

The code looks something like this:

static List<string> GetAllClasses(string nameSpace)
{
    //create an Assembly and use its GetExecutingAssembly Method
    //
http://msdn2.microsoft.com/en-us/library/system.reflection.assembly.getexecutingassembly.aspx
    Assembly asm = Assembly.GetExecutingAssembly();
    //create a list for the namespaces
    List<string> namespaceList = new List<string>();
    //create a list that will hold all the classes
    //the suplied namespace is executing
    List<string> returnList = new List<string>();
    //loop through all the "Types" in the Assembly using
    //the GetType method:
    //
http://msdn2.microsoft.com/en-us/library/system.reflection.assembly.gettypes.aspx
    foreach (Type type in asm.GetTypes())
    {
        if (type.Namespace == nameSpace)
            namespaceList.Add(type.Name);
    }
    //now loop through all the classes returned above and add
    //them to our classesName list
    foreach (String className in namespaceList)
        returnList.Add(className);
    //return the list
    return returnList;
}

In and of itself, this code is valuable enough for me, but doesn't deliver a tangible benefit to my project, so I've taken the time to utilize it as follows:

List<string> list = GetAllClasses("ubercode.Namespace");
foreach (string s in list)
{
    if (s.Contains("BusinessObject"))
    {
        try
        {
            Console.Write(s);
            AzureSds.CreateContainer(s);
            Console.Write(" Created");
        }
        catch
        {
            Console.Write(" Failed");
        }
    }
    else
    {
        Console.Write("don't process:");
        Console.Write(s);
    }
    Console.WriteLine("");
}

Couple that with my AzureSds code, and it's CreateContainer method, I've now automated the creation of all of my containers--based on the class names in a given namespace.

If my code's cooperating with me, I may upload an updated example of my AzureSds code--which is essential for this type of automation.

Thanks PsychoCoder, and thanks Reflection! Without you, I would have had to manually create a few dozen containers. :)

Lazy Code FTW!

No comments: