Create() 공개 정적인 메소드

Build a delegate to return an instance of the specified type given an instance of IocContainer. Finds the public constructor with the most parameters. The resulting method calls the container to resolve each parameter in the constructor.
public static Create ( Type tImpl ) : object>.Func
tImpl System.Type The class to be resolved.
리턴 object>.Func
예제 #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();
        }
예제 #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);
        }
예제 #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)));
        }
예제 #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);
        }
예제 #5
0
 /// <inheritdoc />
 public IRegistration Register(string name, Type tType, Type tImpl)
 {
     return(Register(name, tType, CreateInstanceDelegateFactory.Create(tImpl)));
 }