Catel.IoC.TypeFactory.CreateInstance C# (CSharp) Method

CreateInstance() public method

Creates an instance of the specified type using dependency injection.
The is null.
public CreateInstance ( Type typeToConstruct ) : object
typeToConstruct System.Type The type to construct.
return object
        public object CreateInstance(Type typeToConstruct)
        {
            return CreateInstanceWithTag(typeToConstruct, null);
        }

Usage Example

Example #1
0
        /// <summary>
        /// Resolves an instance of the type registered on the service.
        /// </summary>
        /// <param name="serviceType">The type of the service.</param>
        /// <param name="tag">The tag to register the service with. The default value is <c>null</c>.</param>
        /// <returns>An instance of the type registered on the service.</returns>
        /// <exception cref="ArgumentNullException">The <paramref name="serviceType" /> is <c>null</c>.</exception>
        /// <exception cref="NotSupportedException">The type is not found in any container.</exception>
        /// <remarks>Note that the actual implementation lays in the hands of the IoC technique being used.</remarks>
        public object ResolveType(Type serviceType, object tag = null)
        {
            Argument.IsNotNull("serviceType", serviceType);

            bool isTypeRegistered = false;

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

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

                var error = string.Format("The type '{0}' is not registered", serviceType.FullName);
                Log.Error(error);
                throw new NotSupportedException(error);
            }

            lock (_lockObject)
            {
                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));
            }
        }
All Usage Examples Of Catel.IoC.TypeFactory::CreateInstance