5

how do i register and resolve a generic object/interface in Unity? I'm trying to stay away from the config file.

I'm looking for something like

IEnterpriseClient<IInterface1> to resolve to EnterpriseClient<IInterface1>

The class signature is

class EnterpriseClient<T> : IEnterpriseClient<T> where T : class

Thanks!

William
  • 1,375
  • 12
  • 27

2 Answers2

7

It's pretty much exactly what you'd think:

container.RegisterType<IEnterpriseClient<IInterface1>, EnterpriseClient<IInterface1>>( ... );

That's if you only want that particular closed generic registered. For the open generic (not just IInterface1), you can do:

container.RegisterType(typeof(IEnterpriseClient<>), typeof(EnterpriseClient<>), ... );

You mentioned you'd tried this - what's not working?

Chris Tavares
  • 29,165
  • 4
  • 46
  • 63
2

Look at this question for XML configuration: Unity 2.0 registering generic types via XML

and http://davidhayden.com/blog/dave/archive/2008/03/25/UnityDependencyInjectionOpenGenericTypes.aspx for code configuration.

Community
  • 1
  • 1
Carles Company
  • 7,118
  • 5
  • 49
  • 75
  • I want to stay away from config files and I've tried the simple examples such as _CurrentContainer.RegisterType(typeof(IEnterpriseClient<>), typeof(EnterpriseClient<>) – William May 17 '11 at 14:57