Wednesday, September 20, 2017

.NET IsAssignableFrom

For some reason I have a mental block remembering which way .NET's IsAssignableFrom() function works, so, using the excellent LINQPad and the following code snippet, I came up with the following results:
typeof(BaseClass).IsAssignableFrom(typeof(DerivedClass)) // true
typeof(DerivedClass).IsAssignableFrom(typeof(BaseClass)) // false
Code:
void Main()
{
 typeof(BaseClass).IsAssignableFrom(typeof(DerivedClass)).Dump("typeof(BaseClass).IsAssignableFrom(typeof(DerivedClass))");
 typeof(DerivedClass).IsAssignableFrom(typeof(BaseClass)).Dump("typeof(DerivedClass).IsAssignableFrom(typeof(BaseClass))");
}
    
class BaseClass { }
class DerivedClass : BaseClass { }