public IEnumerable<IRegistration> GetRegistrationsFor(NancyContainer container, Type serviceType) { if (CanCreateRegistrationsFor(serviceType)) { return new IRegistration[] { new TypeRegistration(string.Empty, serviceType, serviceType) }; } return Enumerable.Empty<IRegistration>(); }
public virtual object GetInstance(NancyContainer container) { if (Scope != null) { return Scope.GetInstance(container, this); } return CreateInstance(container); }
public override object CreateInstance(NancyContainer container) { if (Factory == null) { Factory = ImplementationType.CreateFactoryDelegate<object>(container); } return Factory(container); }
public object GetInstance(NancyContainer container, IInstanceCreator creator) { if (creator.Instance == null) { creator.Instance = creator.CreateInstance(container); } return creator.Instance; }
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; }
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>(); }
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>(); }
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>(); }
public override object GetInstance(NancyContainer container) { return Instance; }
public abstract object CreateInstance(NancyContainer container);
public object GetInstance(NancyContainer container, IInstanceCreator creator) { return creator.CreateInstance(container); }
public override object CreateInstance(NancyContainer container) { return Factory(container); }