private void RegisterAsSelf(Type implementationType, ObjLifetime lifetime) { var Entity = from x in Configuration where x.InterfaceType == implementationType select x; if (implementationType.IsInterface) { throw new InvalidOperationException("Cannot register interface as-self."); } if (Entity.Count().Equals(0)) { Configuration.Add(new ContainerEntity() { InterfaceType = implementationType, Implementations = new List <Implementation>() { new Implementation() { ImplType = implementationType, LifeTime = lifetime } } }); return; } throw new InvalidOperationException($"{implementationType.Name} is already registered."); }
private void RegisterCore(Type interfaceType, Type implementationType, ObjLifetime lifetime) { //Registration 'as-self' if (interfaceType == implementationType) { RegisterAsSelf(implementationType, lifetime); return; } ValidateInterfaceAndImplementation(interfaceType, implementationType); var entity = from x in Configuration where x.InterfaceType == interfaceType select x; if (entity.Count().Equals(0)) { Configuration.Add(new ContainerEntity() { Implementations = new List <Implementation>() { new Implementation() { ImplType = implementationType, LifeTime = lifetime } }, InterfaceType = interfaceType, }); return; } if (entity.First().Implementations.FirstOrDefault(x => x.ImplType == implementationType) != null) { return; } entity.First().Implementations.Add(new Implementation() { ImplType = implementationType, LifeTime = lifetime }); }
private Object CreateObjInternal(object[] constructorParams, Type implementationType, Type interfaceType, ObjLifetime lifetime) { object createdObj; if (constructorParams == null) { createdObj = Activator.CreateInstance(implementationType); } else { createdObj = GetConstructor(implementationType).Invoke(constructorParams); } if (lifetime == ObjLifetime.Singleton) { SingletonObjects.Add(new CreatedObject() { ObjType = implementationType, Interface = interfaceType, SingletonInstance = createdObj }); } return(createdObj); }
private Object CreateGenericObject(object[] constructorParams, Type implementationType, Type interfaceType, ObjLifetime lifetime) { //Set object type arguments to ones mentioned in container. return(CreateObjInternal(constructorParams, implementationType.MakeGenericType(interfaceType.GenericTypeArguments), interfaceType, lifetime)); }