Catel.IoC.ServiceLocator.ResolveType C# (CSharp) Method

ResolveType() public method

Resolves an instance of the type registered on the service.
Note that the actual implementation lays in the hands of the IoC technique being used.
The is null. The type is not found in any container.
public ResolveType ( Type serviceType, object tag = null ) : object
serviceType System.Type The type of the service.
tag object The tag to register the service with. The default value is null.
return object
        public object ResolveType(Type serviceType, object tag = null)
        {
            Argument.IsNotNull("serviceType", serviceType);

            lock (_lockObject)
            {
                var isTypeRegistered = IsTypeRegistered(serviceType, tag);

                if (!isTypeRegistered)
                {
                    if (CanResolveNonAbstractTypesWithoutRegistration && serviceType.IsClassEx() && !serviceType.IsAbstractEx())
                    {
                        return _typeFactory.CreateInstance(serviceType);
                    }

                    ThrowTypeNotRegisteredException(serviceType);
                }

                var serviceInfo = new ServiceInfo(serviceType, tag);
                if (_registeredInstances.ContainsKey(serviceInfo))
                {
                    return _registeredInstances[serviceInfo].ImplementingInstance;
                }

                // If a type is registered, the original container is always known
                return ResolveTypeFromKnownContainer(serviceType, tag);
            }
        }

Usage Example

コード例 #1
0
            public void ThrowsArgumentExceptionForNullTypes()
            {
                var serviceLocator = new ServiceLocator();
                var dependencyResolver = serviceLocator.ResolveType<IDependencyResolver>();

                ExceptionTester.CallMethodAndExpectException<ArgumentException>(() => dependencyResolver.CanResolveAll(null));
            }
All Usage Examples Of Catel.IoC.ServiceLocator::ResolveType