src/Mimp.SeeSharper.Reflection/TypeExtensions.Inherit.cs
/// <summary>
/// Check if inherit <paramref name="inheritType"/>. 
/// If <paramref name="type"/> has unspecified generic parameters it will check if all generic parameters are inherit to <paramref name="inheritType"/> generic arguments.
/// </summary>
/// <param name="type"></param>
/// <param name="inheritType"></param>
/// <returns></returns>
/// <exception cref="ArgumentNullException"></exception>
Check if inherit .
If  has unspecified generic parameters it will check if all generic parameters are inherit to  generic arguments.
Check if inherit . If has unspecified generic parameters it will check if all generic parameters are inherit to generic arguments .
public static bool Inherit(this Type type, Type inheritType)
        {
            if (type is null)
                throw new ArgumentNullException(nameof(type));
            if (inheritType is null)
                throw new ArgumentNullException(nameof(inheritType));
            var genericTypeDefinition = inheritType.IsGenericType ? inheritType.GetGenericTypeDefinition() : null;
            return Inherit(type, t =>
            {
                if (t == inheritType)
                    return true;
                if (t.IsGenericType)
                {
                    if (t.GetGenericTypeDefinition() == genericTypeDefinition)
                    {
                        if (inheritType.IsGenericTypeDefinition)
                            return true;
                        var gts = t.GetGenericArguments();
                        var igts = inheritType.GetGenericArguments();
                        for (var i = 0; i < gts.Length; i++)
                        {
                            var gt = gts[i];
                            var igt = igts[i];
                            if (gt.IsGenericParameter)
                            {
                                if (!igt.IsGenericParameter)
                                    return false;
                                if (!gt.Inherit(igt))
                                    return false;
                            }
                            else
                            {
                                if (igt.IsGenericParameter)
                                {
                                    if (!gt.InheritGenericParameter(igt, t))
                                        return false;
                                }
                                else if (gt != igt)
                                    return false;
                            }
                        }
                        return true;
                    }
                }
                return false;
            });
        }
public static bool Inherit ( this Type type , Type inheritType ) { if ( type is null ) throw new ArgumentNullException ( nameof ( type ) ) ; if ( inheritType is null ) throw new ArgumentNullException ( nameof ( inheritType ) ) ; var genericTypeDefinition = inheritType . IsGenericType ? inheritType . GetGenericTypeDefinition ( ) : null ; return Inherit ( type , t => { if ( t == inheritType ) return true ; if ( t . IsGenericType ) { if ( t . GetGenericTypeDefinition ( ) == genericTypeDefinition ) { if ( inheritType . IsGenericTypeDefinition ) return true ; var gts = t . GetGenericArguments ( ) ; var igts = inheritType . GetGenericArguments ( ) ; for ( var i = 0 ; i < gts . Length ; i ++ ) { var gt = gts [ i ] ; var igt = igts [ i ] ; if ( gt . IsGenericParameter ) { if ( ! igt . IsGenericParameter ) return false ; if ( ! gt . Inherit ( igt ) ) return false ; } else { if ( igt . IsGenericParameter ) { if ( ! gt . InheritGenericParameter ( igt , t ) ) return false ; } else if ( gt != igt ) return false ; } } return true ; } } return false ; } ) ; }
Check if inherit .
Check if inherit .
None
None
Type
this
inheritType
Type
None
type
None