IsAssignable what? 01 Oct 2010


I've tweeted it before, Jackson tumblered it in return, but after commenting on a StackOverflow answer where the poster got it wrong, commenting:
I always seem to turn that call around.
I felt that I needed to broaden this grand hack. If you're like me, and apparently like a lot of coders out there, you tend to always get Type.IsAssignableFrom wrong on the first try, then you need this extension method:
public static bool IsAssignableTo (this Type self, Type type)
{
	if (self == null)
		throw new ArgumentNullException ("self");
	if (type == null)
		throw new ArgumentNullException ("type");

	return type.IsAssignableFrom (self);
}
Somehow, even if IsAssignableFrom is more «left to rightish», preserving the side of the arguments a real assignment would have, I understand IsAssignableTo faster. For what it's worth, I've used it extensively while writing our .net 3.5 implementation of System.Linq.Expressions, and it served me well. Have a great friday.