Generics in Unity <p>Hello,</p> <p>I have a question about Unity and the Generics, and I don't know which forum I should post my question. Hope you will be able to help me or redirect my question.</p> <p>I have a caching manager, here are the interface and the class that implement the interface:</p> <p>public interface ICacheMgr&lt;TKey, TValue&gt;</p> <p>public class CacheMgr&lt;TKey, TValue&gt; : ICacheMgr&lt;TKey, TValue&gt;, IDisposable</p> <p> </p> <p>Now I configure them in my app.config:</p> <p>&lt;type name=&quot;CachingMgr&quot;<br/> type=&quot;Acm.Esb.Service.ICacheMgr`2, Acm.Esb.Service&quot;<br/> mapTo=&quot;Acm.Esb.Service.CacheMgr`2, Acm.Esb.Service&quot;&gt;<br/> &lt;typeConfig&gt;<br/> [...]<br/> &lt;/typeConfig&gt; <br/>&lt;/type&gt;</p> <p>Now I try to instantiate a CaheMgr<br/>ICacheMgr&lt;string, int&gt; foo = ((ICacheMgr&lt;string, int&gt;)ServiceContainer.Resolve&lt;ICacheMgr&lt;string, int&gt;&gt;(&quot;CachingMgr&quot;, &quot;production&quot;));<br/>It is running fine.</p> <p>Now, I want to inject object ICacheMgr in another service:<br/>public interface ICacheService<br/>{<br/> ICacheMgr&lt;string, int&gt; CacheMgr { get; set; }<br/>}<br/><br/>public class CacheService : ICacheService<br/>{<br/> public ICacheMgr&lt;string, int&gt; CacheMgr { get; set; }<br/>}<br/><br/><br/>&lt;type name=&quot;CachingService&quot;<br/> type=&quot;ConsoleApplication1.ICacheService, ConsoleApplication1&quot;<br/> mapTo=&quot;ConsoleApplication1.CacheService, ConsoleApplication1&quot;&gt;<br/> &lt;typeConfig&gt;<br/>  &lt;property name=&quot;CacheMgr&quot;<br/>  propertyType=&quot;Acm.Esb.Service.ICacheMgr`2, Acm.Esb.Service&quot;&gt;<br/>   &lt;dependency  name=&quot;CachingMgr&quot;/&gt;<br/>  &lt;/property&gt;<br/> &lt;/typeConfig&gt;<br/>&lt;/type&gt;</p> <p>I don't know how to specify the type of TValue and TKey (here string and int), maybe genericParameterName, but I don't know how to use it when there are 2 generics.</p> <p>Thanks</p>© 2009 Microsoft Corporation. All rights reserved.Wed, 22 Apr 2009 13:06:02 Z59c2bd1c-27e2-4560-b43a-6ff179ef6217http://social.msdn.microsoft.com/Forums/en-US/MEFramework/thread/59c2bd1c-27e2-4560-b43a-6ff179ef6217#59c2bd1c-27e2-4560-b43a-6ff179ef6217http://social.msdn.microsoft.com/Forums/en-US/MEFramework/thread/59c2bd1c-27e2-4560-b43a-6ff179ef6217#59c2bd1c-27e2-4560-b43a-6ff179ef6217chrisqwerhttp://social.msdn.microsoft.com/Profile/en-US/?user=chrisqwerGenerics in Unity <p>Hello,</p> <p>I have a question about Unity and the Generics, and I don't know which forum I should post my question. Hope you will be able to help me or redirect my question.</p> <p>I have a caching manager, here are the interface and the class that implement the interface:</p> <p>public interface ICacheMgr&lt;TKey, TValue&gt;</p> <p>public class CacheMgr&lt;TKey, TValue&gt; : ICacheMgr&lt;TKey, TValue&gt;, IDisposable</p> <p> </p> <p>Now I configure them in my app.config:</p> <p>&lt;type name=&quot;CachingMgr&quot;<br/> type=&quot;Acm.Esb.Service.ICacheMgr`2, Acm.Esb.Service&quot;<br/> mapTo=&quot;Acm.Esb.Service.CacheMgr`2, Acm.Esb.Service&quot;&gt;<br/> &lt;typeConfig&gt;<br/> [...]<br/> &lt;/typeConfig&gt; <br/>&lt;/type&gt;</p> <p>Now I try to instantiate a CaheMgr<br/>ICacheMgr&lt;string, int&gt; foo = ((ICacheMgr&lt;string, int&gt;)ServiceContainer.Resolve&lt;ICacheMgr&lt;string, int&gt;&gt;(&quot;CachingMgr&quot;, &quot;production&quot;));<br/>It is running fine.</p> <p>Now, I want to inject object ICacheMgr in another service:<br/>public interface ICacheService<br/>{<br/> ICacheMgr&lt;string, int&gt; CacheMgr { get; set; }<br/>}<br/><br/>public class CacheService : ICacheService<br/>{<br/> public ICacheMgr&lt;string, int&gt; CacheMgr { get; set; }<br/>}<br/><br/><br/>&lt;type name=&quot;CachingService&quot;<br/> type=&quot;ConsoleApplication1.ICacheService, ConsoleApplication1&quot;<br/> mapTo=&quot;ConsoleApplication1.CacheService, ConsoleApplication1&quot;&gt;<br/> &lt;typeConfig&gt;<br/>  &lt;property name=&quot;CacheMgr&quot;<br/>  propertyType=&quot;Acm.Esb.Service.ICacheMgr`2, Acm.Esb.Service&quot;&gt;<br/>   &lt;dependency  name=&quot;CachingMgr&quot;/&gt;<br/>  &lt;/property&gt;<br/> &lt;/typeConfig&gt;<br/>&lt;/type&gt;</p> <p>I don't know how to specify the type of TValue and TKey (here string and int), maybe genericParameterName, but I don't know how to use it when there are 2 generics.</p> <p>Thanks</p>Mon, 06 Apr 2009 12:59:23 Z2009-04-06T13:02:11Zhttp://social.msdn.microsoft.com/Forums/en-US/MEFramework/thread/59c2bd1c-27e2-4560-b43a-6ff179ef6217#773d7447-4a29-49d8-983c-34e52184532dhttp://social.msdn.microsoft.com/Forums/en-US/MEFramework/thread/59c2bd1c-27e2-4560-b43a-6ff179ef6217#773d7447-4a29-49d8-983c-34e52184532dDavid Libidohttp://social.msdn.microsoft.com/Profile/en-US/?user=David%20LibidoGenerics in Unity I'm currently looking into using Unity and have run into the same problem. I don't think it is genericParameterName, since you can only use it in constructors, properties and methods.Tue, 21 Apr 2009 10:58:23 Z2009-04-21T10:58:23Zhttp://social.msdn.microsoft.com/Forums/en-US/MEFramework/thread/59c2bd1c-27e2-4560-b43a-6ff179ef6217#dabf5758-8deb-4747-a7fc-fe239dee1f5bhttp://social.msdn.microsoft.com/Forums/en-US/MEFramework/thread/59c2bd1c-27e2-4560-b43a-6ff179ef6217#dabf5758-8deb-4747-a7fc-fe239dee1f5bDavid Libidohttp://social.msdn.microsoft.com/Profile/en-US/?user=David%20LibidoGenerics in Unity After some digging I just found the solution, you should be able to use something like this:<br/>&lt;property name=&quot;CacheMgr&quot; propertyType=&quot;Acm.Esb.Service.ICacheMgr`2[[System.String, mscorlib],[System.Int32, mscorlib]], Acm.Esb.Service&quot;&gt;<br/>If you're not sure what type you can use you can just create an instance of the class and get the type from it:<br/>Dictionary&lt;string, int&gt; d = new Dictionary&lt;string, int&gt;();<br/>string t = d.GetType().FullName;<br/>Tue, 21 Apr 2009 11:25:16 Z2009-04-21T11:25:56Z