Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save gongdo/fba727275534beeb678b to your computer and use it in GitHub Desktop.
Save gongdo/fba727275534beeb678b to your computer and use it in GitHub Desktop.
SimpleInjector 3.0.1 can't resolve OpenGeneric collection
using SimpleInjector;
using System;
using System.Diagnostics;
using System.Linq;
namespace ConsoleApplication1
{
public class Foo { }
public class Bar { }
public class Zoo { }
public class Jar { }
public abstract class Selector<T1, T2> { }
public class FooBarSelector : Selector<Foo, Bar> { }
public class ZooJarSelector : Selector<Zoo, Jar> { }
class Program
{
static void Main(string[] args)
{
var container = new Container();
var assemblies = new[] { typeof(Foo).Assembly };
var types = container.GetTypesToRegister(typeof(Selector<,>), assemblies);
var registrations = types.Select(t => Lifestyle.Transient.CreateRegistration(t, container));
container.RegisterCollection(typeof(Selector<,>), registrations);
container.Verify();
// it will be okay:
var fooSelector = container.GetInstance<FooBarSelector>();
var zooSelector = container.GetInstance<ZooJarSelector>();
// but exception thrown bellow:
var fooGenericSelector = container.GetInstance<Selector<Foo, Bar>>();
var zooGenericSelector = container.GetInstance<Selector<Zoo, Jar>>();
Debug.Assert(fooSelector is FooBarSelector
&& zooSelector is ZooJarSelector);
Console.ReadKey();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment