Testing and navigating inheritance C# with Example
interface BaseInterface {} class BaseClass : BaseInterface {} interface DerivedInterface {} class DerivedClass : BaseClass, DerivedInterface {} var baseInterfaceType = typeof(BaseInterface); var derivedInterfaceType = typeof(DerivedInterface); var baseType = typeof(BaseClass); var derivedType = typeof(DerivedClass); var baseInstance = new BaseClass(); var derivedInstance = new DerivedClass(); Console.WriteLine(derivedInstance is DerivedClass); //True Console.WriteLine(derivedInstance is DerivedInterface); //True Console.WriteLine(derivedInstance is BaseClass); //True Console.WriteLine(derivedInstance is BaseInterface); //True Console.WriteLine(derivedInstance is object); //True Console.WriteLine(derivedType.BaseType.Name); //BaseClass Console.WriteLine(baseType.BaseType.Name); //Object Console.WriteLine(typeof(object).BaseType); //null Console.WriteLine(baseType.IsInstanceOfType(derivedInstance)); //True Console.WriteLine(derivedType.IsInstanceOfType(baseInstance)); //False Console.WriteLine( string.Join(",", derivedType.GetInterfaces().Select(t => t.Name).ToArray())); //BaseInterface,DerivedInterface Console.WriteLine(baseInterfaceType.IsAssignableFrom(derivedType)); //True Console.WriteLine(derivedInterfaceType.IsAssignableFrom(derivedType)); //True Console.WriteLine(derivedInterfaceType.IsAssignableFrom(baseType)); //False