コード例 #1
0
        private object ResolveInternal(TypeRegistration registration, ParametersOverloads parameters, IoCOptions options)
        {
            if (options == null)
            {
                options = Options;
            }

            object resolved;

            // Attempt container resolution
            if (TryResolve(out resolved, registration, parameters, options))
            {
                return(resolved);
            }

            // Attempt container resolution
            if (!string.IsNullOrEmpty(registration.Name))
            {
                // Fail if requesting named resolution and settings set to fail if unresolved
                if (options.NamedResolutionFailureAction == NamedResolutionFailureAction.Fail)
                {
                    IoCResolutionException.Raise(registration.Type);
                }

                // Attemped unnamed fallback container resolution if relevant and requested
                if (options.NamedResolutionFailureAction == NamedResolutionFailureAction.AttemptUnnamedResolution)
                {
                    if (TryResolve(out resolved, new TypeRegistration(registration.Type), parameters, options))
                    {
                        return(resolved);
                    }
                }
            }

#if EXPRESSIONS
            // Attempt to construct an automatic lazy factory if possible
            if (IsAutomaticLazyFactoryRequest(registration.Type))
            {
                return(GetLazyAutomaticFactoryRequest(registration.Type));
            }
#endif
#if ENUMERABLE_REQUESTS
            if (IsIEnumerableRequest(registration.Type))
            {
                return(GetIEnumerableRequest(registration.Type));
            }
#endif
            // Attempt unregistered construction if possible and requested
            if ((options.UnregisteredResolutionAction == UnregisteredResolutionAction.AttemptResolve)
                /*|| (registration.Type.IsGenericType() && options.UnregisteredResolutionAction == UnregisteredResolutionAction.GenericsOnly*/)
            {
                if (!registration.Type.IsAbstract() && !registration.Type.IsInterface())
                {
                    return(ConstructType(registration.Type, registration.Type, null, parameters));
                }
            }

            // Unable to resolve - throw
            return(IoCResolutionException.Raise(registration.Type));
        }
コード例 #2
0
        public static Never Raise(Type unresolvedType, string messageFormat, Exception innerException = null)
        {
            var message = String.Format(messageFormat ?? DEFAULT_FORMAT, unresolvedType?.FullName);

            IoCResolutionException ex;

            if (innerException != null)
            {
                ex = new IoCResolutionException(unresolvedType, message, innerException);
            }
            else
            {
                ex = new IoCResolutionException(unresolvedType, message);
            }
            throw ex;
        }
コード例 #3
0
        private bool TryResolve(out object resolved, TypeRegistration registration, ParametersOverloads parameters, IoCOptions options)
        {
            for (var container = this; container != null; container = container.Parent)
            {
                IoCFactory factory;
                if (registry.TryGetValue(registration, out factory))
                {
                    try {
                        resolved = factory.GetObject(registration.Type, this, parameters);
                        return(true);
                    }
                    catch (IoCResolutionException) {
                        throw;
                    }
                    catch (Exception ex) {
                        IoCResolutionException.Raise(registration.Type, ex);
                    }
                }

#if RESOLVE_OPEN_GENERICS
                // Attempt container resolution of open generic
                if (registration.Type.IsGenericType())
                {
                    var openTypeRegistration = new TypeRegistration(registration.Type.GetGenericTypeDefinition(),
                                                                    registration.Name);

                    if (_RegisteredTypes.TryGetValue(openTypeRegistration, out factory))
                    {
                        try {
                            return(factory.GetObject(registration.Type, this, parameters, options));
                        }
                        catch (TinyIoCResolutionException) {
                            throw;
                        }
                        catch (Exception ex) {
                            IoCResolutionException.Raise(registration.Type, ex);
                        }
                    }
                }
#endif
            }

            resolved = null;
            return(false);
        }