Wednesday, July 30, 2008

Recursively Searching for Classes of Specified Type from an Assembly or Type

Sometimes I find myself implementing a plugin architecture and I need to find a list of classes in an assembly that qualify as plugins for a given project.  I wrote the following class to help me do this:

public static class TypeFinder
{
    public static List<Type> GetTypesFromAssembly(Assembly assembly, params Type[] assignableTypes)
    {
        List<Type> types = new List<Type>();
 
        if (assembly != null)
            foreach (Type type in assembly.GetTypes())
                foreach (Type baseType in GetBaseTypesFromType(type, assignableTypes))
                    if (!types.Contains(baseType)) types.Add(baseType);
 
        return types;
    }
 
    public static List<Type> GetBaseTypesFromType(Type type, params Type[] assignableTypes)
    {
        List<Type> types = new List<Type>();
 
        foreach (Type assignableType in assignableTypes)
        {
            if (assignableType != type && assignableType.IsAssignableFrom(type))
            {
                types.Add(type);
                break;
            }
        }
 
        if (type.BaseType != null)
           types.AddRange(GetBaseTypesFromType(type.BaseType, assignableTypes));
 
        return types;
    }
}


Here's what the call looks like:
List<Type> plugins = TypeFinder.GetTypesFromAssembly(assembly, typeof(IMyPlugin), typeof(MyAbstractClass), typeof(MyBaseClass);

© 2008, D. Patrick Caldwell, Vice President for Research and Development, Emerald Software Group, LLC

Monday, July 28, 2008

Writing Anonymous Methods with Lambda Expressions

I like using Lambda expressions for anonymous methods. I don't really have a good reason for it, but I like it, so here's the difference. Let's say you have a class exposing the following delegate:

public delegate bool CheckExpirationDelegate();
public CheckExpirationDelegate CheckExpiration;



The normal anonymous delegate would look like this:
di.CheckExpiration += delegate() { return true; };


But, you can also do it like this with lambda expressions:
di.CheckExpiration += () => true;




Here's another anonymous method, but you can use this one without a delegate:
Func<string, char, string> GreetTheWorld =
    (greeting, punctuation) =>
        string.Format("{0} World{1}", greeting, punctuation);
    Console.WriteLine(GreetTheWorld("Hello", "!"));




There are many other great uses for lambda expressions . . . fodder for future posts.


© 2008, D. Patrick Caldwell, Vice President for Research and Development, Emerald Software Group, LLC

Sunday, July 13, 2008

Silence of the Lambs


if (!it.skin.apply(TheLotion))
{
it.HoseCount++;
};


More computer humor

To be or not to be?


string TheQuestion = "2B || !2B";


More computer humor

All work and no play . . .


if (WorkQuantity == "All" && PlayQuantity == "None")
{
Jack.BoyType = BoyTypes.Dull;
};



More computer humor

Bottles of Beer on the Wall


BottlesOfBeerOnTheWall(new Wall(99));

public int BottlesOfBeerOnTheWall(Wall TheWall)
{
TheWall.BottlesOfBeer.TakeOneDown().PassItAround();

if (TheWall.BottlesOfBeer.Count > 0)
{
return BottlesOfBeerOnTheWall(TheWall);
}

return 0;
}



More computer humor

I only allow authenticated rodents . . . sorry.

2007/09/18: "Unauthenticated user, Please check if virtual directory of <name changed to protect guilty> Server anonymouse access is disabled." 

More computer humor

Well, that pretty much narrows it down.

2007/08/09: "Syntax error, permission violation, or other nonspecific error."

More computer humor

So that pretty much rules out 0 then I guess?

2007/10/17: "One or more component failed validation."

More computer humor

Thursday, July 10, 2008

Hello world!

So this was my auto-generated first post.  I decided not to delete the post because it is aptly titled, "Hello world!"  As it turns out, my blog (YATB) is going to be yet another techie blog.

I'm a software engineer and stuff so most of my posts will be about just that . . . software.  I am also on the management team with my company so there will be various observations about business and leadership as well.

With that, here is my contribution to the interwebs . . . Patrick Caldwell's Weblog