GetInstance() публичный Метод

public GetInstance ( ) : object
Результат object
Пример #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);
        }
Пример #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);
        }
Пример #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);
        }