Polymorphic resolution of common parameters in Unity registerType

I am trying to do the following and not execute:

class base {}

class derived1 : base {}

class derived2 : base {}

interface itr<t>
where t : class, base
{}

class c1: itr<derived1>
{}

class c2 : itr<derived2>
{}  

// Failed to complete the following 2 registrations:

_unityContainer.RegisterType<itr<base>, c1>("c1");

_unityContainer.RegisterType<itr<base>, c2>("c2");

The error I get is that the second parameter in the registrations above cannot be specified in the first parameter, and this registration is not valid. Any suggestions on how I can do this?
I need to do the above, instead of registering in the classes of derivatives1 or derivatives2 as general parameters, because when resolving, I don’t want to know the exact derived type that I solve. I just want to work with base type methods polymorphically.

+3
1

, . itr<derived1> itr<base>, derived1 base.

, , List<T> class:

List<string> list1 = new List<string>();
list1.Add("Hello, World!");

// This cast will fail, because the following line would then be legal:
List<object> list2 = (List<object>)list1; 

// ints are objects, but they are not strings!
list2.Add(1);

, list1[1] int , string s.

, , .

( where t : class, base class. base , class .)

+5

Source: https://habr.com/ru/post/1776736/


All Articles