src/Mimp.SeeSharper.Reflection/TypeExtensions.Generic.cs
/// <summary>
/// Resolve all possible combinations with generics of <paramref name="inheritGenericType"/>.
/// </summary>
/// <param name="type"></param>
/// <param name="inheritGenericType"></param>
/// <returns></returns>
/// <exception cref="ArgumentNullException"></exception>
/// <exception cref="ArgumentException">If <paramref name="type"/> or <paramref name="inheritGenericType"/> isn't a generic type definition.</exception>
/// <exception cref="InvalidOperationException">If a generic argument has ambigous resolving types.</exception>
Resolve all possible combinations with generics of .
Resolve all possible combinations with generics of .
public static IEnumerable<Type> ResolveInheritGenericTypes(this Type type, Type inheritGenericType)
        {
            if (type is null)
                throw new ArgumentNullException(nameof(type));
            if (inheritGenericType is null)
                throw new ArgumentNullException(nameof(inheritGenericType));
            if (!type.IsGenericType)
                throw new ArgumentException($"{type} isn't a generic type.", nameof(type));
            if (!type.IsGenericType)
                throw new ArgumentException($"{inheritGenericType} isn't a generic type.", nameof(inheritGenericType));
            var genericTypeDefinition = type.GetGenericTypeDefinition();
            var args = type.GetGenericArguments();
            var inheritArgs = inheritGenericType.GetGenericArguments();
            foreach (var genericType in type.GetGenericTypes(inheritGenericType.GetGenericTypeDefinition()))
                if (genericType.HasGenericParameters())
                {
                    var genericArgs = genericType.GetGenericArguments();
                    var genericTypes = new Type[args.Length];
                    for (int i = 0; i < args.Length; i++)
                    {
                        var arg = args[i];
                        for (int j = 0; j < genericArgs.Length; j++)
                            if (genericArgs[j] == arg)
                            {
                                var t = genericTypes[i];
                                var it = inheritArgs[j];
                                if (t is null)
                                    genericTypes[i] = it;
                                else if (t != it)
                                    throw new InvalidOperationException($@"{arg} has ambiguous types for resolving ""{type}"" to ""{inheritGenericType}"": {t} - {it}");
                            }
                        if (genericTypes[i] is null)
                            genericTypes[i] = arg;
                    }
                    yield return genericTypeDefinition.MakeGenericType(genericTypes);
                }
        }
public static IEnumerable < Type > ResolveInheritGenericTypes ( this Type type , Type inheritGenericType ) { if ( type is null ) throw new ArgumentNullException ( nameof ( type ) ) ; if ( inheritGenericType is null ) throw new ArgumentNullException ( nameof ( inheritGenericType ) ) ; if ( ! type . IsGenericType ) throw new ArgumentException ( $" { type }  isn't a generic type. " , nameof ( type ) ) ; if ( ! type . IsGenericType ) throw new ArgumentException ( $" { inheritGenericType }  isn't a generic type. " , nameof ( inheritGenericType ) ) ; var genericTypeDefinition = type . GetGenericTypeDefinition ( ) ; var args = type . GetGenericArguments ( ) ; var inheritArgs = inheritGenericType . GetGenericArguments ( ) ; foreach ( var genericType in type . GetGenericTypes ( inheritGenericType . GetGenericTypeDefinition ( ) ) ) if ( genericType . HasGenericParameters ( ) ) { var genericArgs = genericType . GetGenericArguments ( ) ; var genericTypes = new Type [ args . Length ] ; for ( int i = 0 ; i < args . Length ; i ++ ) { var arg = args [ i ] ; for ( int j = 0 ; j < genericArgs . Length ; j ++ ) if ( genericArgs [ j ] == arg ) { var t = genericTypes [ i ] ; var it = inheritArgs [ j ] ; if ( t is null ) genericTypes [ i ] = it ; else if ( t != it ) throw new InvalidOperationException ( $@" { arg }  has ambiguous types for resolving  "" { type } ""  to  "" { inheritGenericType } "" :  { t }  -  { it } " ) ; } if ( genericTypes [ i ] is null ) genericTypes [ i ] = arg ; } yield return genericTypeDefinition . MakeGenericType ( genericTypes ) ; } }
Resolve all possible combinations with generics of .
Resolve all possible combinations with generics of .
None
None
If  or  isn't a generic type definition.
If or isn ' t a generic type definition .
If a generic argument has ambigous resolving types.
If a generic argument has ambigous resolving types .
Type
this
inheritGenericType
Type
None
type
None