示例#1
0
 public IEnumerable<IRegistration> GetRegistrationsFor(NancyContainer container, Type serviceType)
 {
     if (CanCreateRegistrationsFor(serviceType))
     {
         return new IRegistration[] { new TypeRegistration(string.Empty, serviceType, serviceType) };
     }
     return Enumerable.Empty<IRegistration>();
 }
示例#2
0
 public virtual object GetInstance(NancyContainer container)
 {
     if (Scope != null)
     {
         return Scope.GetInstance(container, this);
     }
     return CreateInstance(container);
 }
示例#3
0
 public override object CreateInstance(NancyContainer container)
 {
     if (Factory == null)
     {
         Factory = ImplementationType.CreateFactoryDelegate<object>(container);
     }
     return Factory(container);
 }
示例#4
0
 public object GetInstance(NancyContainer container, IInstanceCreator creator)
 {
     if (creator.Instance == null)
     {
         creator.Instance = creator.CreateInstance(container);
     }
     return creator.Instance;
 }
示例#5
0
        public static object CreateDelegate(Type delegateType, NancyContainer nancyContainer, string serviceName)
        {
            var returnType = delegateType.FunctionReturnType();
            var exps = new Expression[] {
                Expression.Constant(serviceName),
                Expression.Constant(returnType)
            };

            var container = Expression.Constant(nancyContainer);
            var exp = Expression.Call(container, nancyContainer.GetMethod(nc => nc.Resolve(string.Empty, default(Type))), exps);

            var expConvert = Expression.Convert(exp, returnType);

            var g = Expression.Lambda(delegateType, expConvert).Compile();

            return (object)g;
        }
示例#6
0
        public IEnumerable<IRegistration> GetRegistrationsFor(NancyContainer container, Type serviceType)
        {
            if (serviceType != null && serviceType.IsGenericTypeDefinedBy(typeof(Func<>)))
            {
                var baseServiceType = serviceType.FunctionReturnType();
                var baseServiceRegistrations = container.GetRegistrations(baseServiceType, null);

                var newRegistrations = new List<IRegistration>();
                foreach (var registration in baseServiceRegistrations)
                {
                    var factory = FuncGenerator.CreateDelegate(serviceType, container, registration.Name);

                    newRegistrations.Add(new FactoryRegistration(serviceType, c => factory, registration.Name));
                }
                return newRegistrations.AsEnumerable();
            }

            return Enumerable.Empty<IRegistration>();
        }
示例#7
0
        public IEnumerable<IRegistration> GetRegistrationsFor(NancyContainer container, Type serviceType)
        {
            if (serviceType != null)
            {
                Type elementType = null;
                Func<INancyContainer, object> delegateActivator = null;
                if (serviceType.IsGenericTypeDefinedBy(typeof(IEnumerable<>)))
                {
                    elementType = serviceType.GetGenericArguments()[0];
                }
                else if (serviceType.IsArray)
                {
                    elementType = serviceType.GetElementType();
                }

                if (elementType != null)
                {
                    var elementArrayType = elementType.MakeArrayType();

                    delegateActivator = (c) =>
                    {
                        var nc = c as NancyContainer;
                        if (nc != null)
                        {
                            var registrations = nc.GetRegistrations(elementType, null).ToArray();
                            var elements = registrations.Select(r => r.GetInstance(nc)).ToArray();
                            var result = Array.CreateInstance(elementType, elements.Length);
                            elements.CopyTo(result, 0);
                            return result;
                        }

                        return null;
                    };

                    return new IRegistration[] { new FactoryRegistration(serviceType, delegateActivator, null) };
                }

                if (delegateActivator != null) return new IRegistration[] { new FactoryRegistration(serviceType, delegateActivator, null) };
            }

            return Enumerable.Empty<IRegistration>();
        }
示例#8
0
        public IEnumerable<IRegistration> GetRegistrationsFor(NancyContainer container, Type serviceType)
        {
            if (CanCreateRegistrationsFor(serviceType))
            {
                Type genericTypeDefinition = serviceType.GetGenericTypeDefinition();
                Type[] genericArguments = serviceType.GetGenericArguments();

                var newRegistrations = new List<IRegistration>();
                foreach (var registration in container.GetRegistrations(genericTypeDefinition, null))
                {
                    var typeRegistration = registration as TypeRegistration;
                    if (typeRegistration != null)
                    {
                        var implType = typeRegistration.ImplementationType.MakeGenericType(genericArguments);
                        newRegistrations.Add(new TypeRegistration(registration.Name, serviceType, implType));
                    }
                }
                return newRegistrations.AsEnumerable();
            }

            return Enumerable.Empty<IRegistration>();
        }
示例#9
0
 public override object GetInstance(NancyContainer container)
 {
     return Instance;
 }
示例#10
0
 public abstract object CreateInstance(NancyContainer container);
示例#11
0
 public object GetInstance(NancyContainer container, IInstanceCreator creator)
 {
     return creator.CreateInstance(container);
 }
示例#12
0
 public override object CreateInstance(NancyContainer container)
 {
     return Factory(container);
 }