Tim Van Wassenhove

Passionate geek, interested in Technology. Proud father of two

21 Jul 2010

Quick reminder about the workings of Type.IsAssignableFrom

Here is a quick reminder about the workings of Type.IsAssignableFrom

class Fruit {}
class Banana : Fruit {}

public void CanAssignBananaToFruit()
	var fruit = typeof (Fruit);
	var banana = typeof (Banana);

public void CanNotAssignFruitToBanana()
	var fruit = typeof(Fruit);
	var banana = typeof(Banana);

I really hate this API because it always seems backward to me. Here is how i really want to use it


With the aid of an extension method we can easily achieve this

public static bool CanBeAssignedTo(this Type sourceType, Type destinationType)
	return destinationType.IsAssignableFrom(sourceType);