为什么 IContainer.IsRegistered(Type serviceType) 添加注册?
Type serviceType = typeof (string[]);
int rc = container.ComponentRegistry.Registrations.Count();
container.IsRegistered(serviceType);
int rc2 = container.ComponentRegistry.Registrations.Count();
Assert.AreEqual(rc, rc2);
上述行为可能会产生以下副作用:
public class Test
{
public Entity[] Entities { get; set; }
}
//...
var bldr = new ContainerBuilder();
bldr.RegisterModule<ArraysInjectionGuardModule>();
var container = bldr.Build();
var t = new Test();
container.InjectProperties(t);
Assert.IsNull(t.Entities);
因为container.InjectProperties(...);
调用container.IsRegistered(..)
并typeof(Entity[])
作为参数传递,所以 t.Entities 使用空数组进行初始化。当我发现这种行为时,我有点困惑。