Nested contravariant generics #404
-
Is it possible to satisfy the following requirement using DryIoc? I am trying to implement a simple event handler where A is base of B and anything related to B should trigger A as well. This is like what https://github.com/dadhi/DryIoc/blob/master/docs/DryIoc.Docs/OpenGenerics.md#generic-variance-when-resolving-many-services has as an example but the example there is covariant with the E.g.: public static class DryIocContainer_GenericVarianceTest
{
public static void Run()
{
var c = new Container();
c.Register<IGenericType<A>, GenericType<A>>();
c.Register<IGenericType<B>, GenericType<B>>();
c.Register<IGenericTypeWrapper<IGenericType<A>>, GenericTypeWrapper<IGenericType<A>>>();
c.Register<IGenericTypeWrapper<IGenericType<B>>, GenericTypeWrapper<IGenericType<B>>>();
var genericTypes_A = c.ResolveMany<IGenericType<A>>(); // Gets A and B (expected)
var genericTypes_B = c.ResolveMany<IGenericType<B>>(); // Gets B (expected)
var genericTypeWrappers_A = c.ResolveMany<IGenericTypeWrapper<IGenericType<A>>>(); // Gets A (unexpected)
var genericTypeWrappers_B = c.ResolveMany<IGenericTypeWrapper<IGenericType<B>>>(); // Gets A and B (unexpected)
}
}
public interface IGenericTypeWrapper<in T> where T : class
{
}
public interface IGenericType<in T> where T : class
{
void Do(T value);
}
public class GenericTypeWrapper<T> : IGenericTypeWrapper<T> where T : class
{
}
public class GenericType<T> : IGenericType<T> where T : class
{
public void Do(T value)
{
}
}
public class A : IGenericType<A>
{
public virtual void Do(A value)
{
// Do A
}
}
public class B : A
{
public override void Do(A value)
{
// Do B
}
} |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 14 replies
-
Interesting question, will look |
Beta Was this translation helpful? Give feedback.
-
Hey @dadhi I have another question regarding open generics so thought it might be a better idea to continue within the same discussion. Just let me know if it's better for you if I create a new discussion. My question is whether it is funny to try to do something like the following with DryIoc :)
I know it is super ridiculous because |
Beta Was this translation helpful? Give feedback.
Interesting question, will look