GetInstance() public method

public GetInstance ( ) : object
return object
Esempio n. 1
0
        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);
        }
Esempio n. 2
0
        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);
        }
Esempio n. 3
0
        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);
        }