我试图执行以下操作:
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));
答案 0 :(得分:2)
您需要使用非通用RegisterType
,它可能看起来像这样:
container.RegisterType(typeof(ITabItem), item, item.FullName);