使用Reflection的Unity寄存器类型

时间:2016-04-25 14:30:18

标签: c# reflection ioc-container unity3d

我试图执行以下操作:

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

但我在item方法中RegisterType收到此错误:

错误CS0118&#39; item&#39;是一个变量但是像类型一样使用

我知道我传递的是对象类型,但我该如何解决这个问题?

编辑:这是非动态的方式。

container.RegisterType<ITabItem, AdminViewModel>(nameof(AdminViewModel));
container.RegisterType<ITabItem, OwnerViewModel>(nameof(OwnerViewModel));

1 个答案:

答案 0 :(得分:2)

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

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