2

I'm trying to do the following:

foreach (var item in Assembly.GetExecutingAssembly()
                             .GetTypes()
                             .Where(i => i.GetInterfaces()
                             .Contains(typeof(ITabItem))))
{
    container.RegisterType<ITabItem, item>(nameof(item));
}

But I'm getting this error on item in the RegisterType method:

Error CS0118 'item' is a variable but is used like a type

I know that I'm passing an object type but how do I get around this?

EDIT: this is the non-dynamic way that works.

container.RegisterType<ITabItem, AdminViewModel>(nameof(AdminViewModel));
container.RegisterType<ITabItem, OwnerViewModel>(nameof(OwnerViewModel));
grmbl
  • 2,514
  • 4
  • 29
  • 54
  • I believe the referred question goes deeper into generics and reflection in general but my question addresses a Unity specific method and the answer for my question specific could not be found in this question. The answer below on the other hand does. So I do not believe my question is a duplicate. Marking this question as duplicate could lead people away from the actual answer below... – grmbl Apr 26 '16 at 09:54

1 Answers1

2

You need to use the non-generic RegisterType, which probably looks something like this:

container.RegisterType(typeof(ITabItem), item, item.FullName);
vcsjones
  • 138,677
  • 31
  • 291
  • 286