simpleinjector / SimpleInjector

An easy, flexible, and fast Dependency Injection library that promotes best practice to steer developers towards the pit of success.
https://simpleinjector.org
MIT License
1.21k stars 154 forks source link

Open-generic registrations for types with generic array generic type arguments can't be resolved #996

Closed dotnetjunkie closed 5 months ago

dotnetjunkie commented 5 months ago

Describe the bug

Consider the following type:

public class GenericArrayQueryHandler<T> : IQueryHandler<GenericArrayQuery<T>, Tuple<T>[]> { }

This type is generic, and it's generic type T goes into the generic type arguments of the implementing interface. One of the types, Tuple<T>[] is a 'generic array', as in, its element type is generic.

Simple Injector can't handle this specific case when, and only when, the type is registered as open generic.

The following type, on the other hand, doesn't exhibit the problem:

public class GenericArrayQueryHandler<T> : IQueryHandler<GenericArrayQuery<T>, List<Tuple<T>>> { }

Expected behavior

The type GenericArrayQueryHandler<int> is created and resolved by Simple Injector.

Actual behavior

The following exception is thrown:

System.InvalidOperationException: This operation is only valid on generic types.

Not only should an exception not be thrown, the exception is highly generic and confusing.

To Reproduce

The following code reproduces the issue:

var container = new Container();

container.Register(typeof(IQueryHandler<,>), typeof(GenericArrayQueryHandler<>));

container.GetInstance<IQueryHandler<GenericArrayQuery<int>, Tuple<int>[]>>();

public class GenericArrayQuery<T> : IQuery<Tuple<T>[]> { }
public class GenericArrayQueryHandler<T> : IQueryHandler<GenericArrayQuery<T>, Tuple<T>[]> { }

Additional context

Simple Injector v5.4.4.