IsDerived
Checks whether the type is derived from specified type or implemented of specified interface.
Source
public static bool IsDerived<T>(this Type type)
{
Type baseType = typeof(T);
if(baseType.FullName == type.FullName)
{
return true;
}
if(type.IsClass)
{
return baseType.IsClass
? type.IsSubclassOf(baseType)
: baseType.IsInterface
? IsImplemented(type, baseType)
: false;
}
else if(type.IsInterface && baseType.IsInterface)
{
return IsImplemented(type, baseType);
}
return false;
}
public static bool IsImplemented(Type type, Type baseType)
{
Type[] faces = type.GetInterfaces();
foreach(Type face in faces)
{
if(baseType.Name.Equals(face.Name))
{
return true;
}
}
return false;
}
Example
if(typeof(UserEntity).IsDerived<IExtend>())
{
// do something..
}