public object GetInstance(Type componentType) { object instance = null; if (_registeredComponents.ContainsKey(componentType)) { object generatedInstance = _registeredComponents[componentType].GetInstance(); if (componentType.Equals(generatedInstance.GetType())) { instance = _registeredComponents[componentType].GetInstance(); } else { throw new InvalidCastException("The stored instance cannot be cast to the given type."); } } else { RegisteredComponent registeredComponent = new RegisteredComponent(this, componentType); _registeredComponents[componentType] = registeredComponent; instance = registeredComponent.GetInstance(); } return(instance); }
public ComponentType GetInstance <ComponentType>() { Type componentType = typeof(ComponentType); ComponentType instance = default(ComponentType); if (_registeredComponents.ContainsKey(componentType)) { if (_registeredComponents[componentType].GetInstance() is ComponentType) { instance = (ComponentType)_registeredComponents[componentType].GetInstance(); } else { throw new InvalidCastException("The stored instance cannot be cast to the given type."); } } else { RegisteredComponent registeredComponent = new RegisteredComponent(this, componentType); _registeredComponents[componentType] = registeredComponent; instance = (ComponentType)registeredComponent.GetInstance(); } return(instance); }
public void RegisterComponent <ComponentType>(ComponentType instance) { Type componentType = typeof(ComponentType); RegisteredComponent registeredComponent = new RegisteredComponent(this, instance); _registeredComponents.Add(componentType, registeredComponent); }
public void RegisterComponent <ComponentType>(params RegisteredParameter[] parameters) { Type componentType = typeof(ComponentType); RegisteredParameters registeredParameters = new RegisteredParameters(parameters); RegisteredComponent registeredComponent = new RegisteredComponent(this, componentType, registeredParameters); _registeredComponents.Add(componentType, registeredComponent); }
public void RegisterComponent <ComponentAbstractType, ComponentConcreteType>() { Type abstractType = typeof(ComponentAbstractType); Type concreteType = typeof(ComponentConcreteType); if (concreteType.IsAbstract || concreteType.IsInterface) { throw new NotSupportedException("The provided type is not a concrete type."); } RegisteredComponent registeredComponent = new RegisteredComponent(this, concreteType); _registeredComponents.Add(abstractType, registeredComponent); }
public object GetInstance(Type componentType, Dictionary <Type, int> attemptedInstances) { object instance = null; if (_registeredComponents.ContainsKey(componentType)) { instance = _registeredComponents[componentType].GetInstance(attemptedInstances); } else { RegisteredComponent registeredComponent = new RegisteredComponent(this, componentType); _registeredComponents[componentType] = registeredComponent; instance = registeredComponent.GetInstance(attemptedInstances); } return(instance); }