unitycontainer / configuration

Unity.Configuration package
Apache License 2.0
13 stars 13 forks source link

How to Resolve Generic Class<T>(it's constructor parameter also generic…e.g:Param<T> param) With Unity Container?? help! thanks #27

Open jetjo opened 4 years ago

jetjo commented 4 years ago
interface Interface<T>{}

class Class<T>:Interface<T>
{
      public Class(IInterfaceParam<T> param){}
}

interface IInterfaceParam<T>{}

class InterfaceParam<T>:IInterfaceParam<T>{}

class Tclass{}

 class Program
 {
          void Main(){
                  var v1= container.Resolve<Interface<Tclass>>();
          }
 }

how to config in unity.config xml file?? help! thanks!

bellow is my config:

  <register type="IInterfaceParam`1" mapTo="InterfaceParam`1" name="paramDefault">
  </register>

  <register type="Interface`1" mapTo="Class`1">
    <constructor>
      <param type="IInterfaceParam`1" name="param">
        <dependency name="paramDefault"></dependency>
      </param>
    </constructor>
  </register>

but i got the exception:InvalidOperationException: Configuration is incorrect, the type Class`1 does not have a constructor that takes parameters named param...why???

jetjo commented 4 years ago

can help me?? thanks very much!

ENikS commented 4 years ago

References: https://msdn.microsoft.com/en-us/library/ff647202.aspx https://msdn.microsoft.com/en-us/library/ff660914(v=pandp.20).aspx https://msdn.microsoft.com/en-us/library/dn223671(v=pandp.30).aspx

ENikS commented 4 years ago

Not sure if it is the case, but 'param' is a reserved word in c#. Perhaps try changing the name?