This class provides a method to build a delegate to create a specified type. It is used by Register<TType, TImp>() to build the Func<IDependencyResolver, TImp> need to create the instance to be returned. Also used by the Resolve methods if the type requested is a class, not an interface, and is not currently Registered in the container.
Esempio n. 1
0
        /// <include file='XmlDocumentation/IDependencyResolver.xml' path='IDependencyResolver/Members[@name="Resolve4"]/*' />
        public object Resolve(string name, Type type)
        {
            try
            {
                return(typeRegistry.Get(name, type).GetInstance());
            }
            catch (KeyNotFoundException)
            {
                if (type.IsClass)
                {
                    try
                    {
                        var func = CreateInstanceDelegateFactory.Create(type);
                        Register(name, type, func);
                        return(func(this));
                    }
                    catch
                    {
                        throw new KeyNotFoundException();
                    }
                }
            }

            throw new KeyNotFoundException();
        }
Esempio n. 2
0
        private object HandleUnResolved(string name, Type type)
        {
            if (type.IsGenericType)
            {
                var result = ResolveUsingOpenType(name, type);
                if (result != null)
                {
                    return(result);
                }
            }

            if (type.IsClass)
            {
                var func = CreateInstanceDelegateFactory.Create(type);
                Register(name, type, func);
                // Thanks to dfullerton for catching this.
                return(typeRegistry.Get(name, type).GetInstance());
            }

            if (type.IsInterface)
            {
                var regs = typeRegistry.GetDerived(name, type);
                var reg  = regs.FirstOrDefault();
                if (reg != null)
                {
                    var instance = reg.GetInstance();
                    Register(name, type, (c) => c.Resolve(name, instance.GetType()));
                    return(instance);
                }
            }
            return(null);
        }
Esempio n. 3
0
        /// <summary>
        /// Registers the specified name.
        /// </summary>
        /// <param name="name">The name.</param>
        /// <param name="tType">Type of the t.</param>
        /// <param name="tImpl">The t implementation.</param>
        /// <returns></returns>
        public IRegistration Register(string name, Type tType, Type tImpl)
        {
            if (tType.ContainsGenericParameters)
            {
                return(RegisterOpenType(name, tType, tImpl));
            }

            return(Register(name, tType, CreateInstanceDelegateFactory.Create(tImpl)));
        }
Esempio n. 4
0
        private object HandleUnResolved(Exception knfe, string name, Type type)
        {
            if (type.IsGenericType)
            {
                object result = ResolveUsingOpenType(knfe, name, type);
                if (result != null)
                {
                    return(result);
                }
            }

            if (type.IsClass)
            {
                try
                {
                    var func = CreateInstanceDelegateFactory.Create(type);
                    Register(name, type, func);
                    // Thanks to dfullerton for catching this.
                    return(typeRegistry.Get(name, type).GetInstance());
                }
                catch
                {
                    throw new KeyNotFoundException(ResolveFailureMessage(type), knfe);
                }
            }

            if (type.IsInterface)
            {
                var regs = typeRegistry.GetDerived(name, type);
                var reg  = regs.FirstOrDefault();
                if (reg != null)
                {
                    object instance = reg.GetInstance();
                    Register(name, type, (c) => c.Resolve(name, instance.GetType()));
                    return(instance);
                }
                else
                {
                    throw new KeyNotFoundException(ResolveFailureMessage(type), knfe);
                }
            }
            throw new KeyNotFoundException(ResolveFailureMessage(type), knfe);
        }
Esempio n. 5
0
 /// <inheritdoc />
 public IRegistration Register(string name, Type tType, Type tImpl)
 {
     return(Register(name, tType, CreateInstanceDelegateFactory.Create(tImpl)));
 }