简体   繁体   中英

Unity register types using Reflection

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));

您需要使用非通用RegisterType ,它可能看起来像这样:

container.RegisterType(typeof(ITabItem), item, item.FullName);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM