Example #1
0
        public static TInst GetRegisteredInstance <TInst>(this Container container)
            where TInst : class
        {
            var registration = container.GetCurrentRegistrations().FirstOrDefault(x => x.ServiceType == typeof(TInst));

            if (registration == null)
            {
                return(null);
            }
            var creatorField = registration.GetType().GetFields(BindingFlags.NonPublic | BindingFlags.Instance)
                               .FirstOrDefault(x => x.Name == "instanceCreator");

            if (creatorField == null)
            {
                return(null);
            }
            var fnc = creatorField.GetValue(registration) as Func <object>;

            if (fnc == null)
            {
                return(null);
            }
            var cfg = fnc() as TInst;

            return(cfg);
        }
        private static InstanceProducer[] FindAssignableRegistrations(Container container, 
            Type serviceType)
        {
            Type serviceTypeDefinition = serviceType.GetGenericTypeDefinition();

            return (
                from reg in container.GetCurrentRegistrations()
                where reg.ServiceType.IsGenericType
                where reg.ServiceType.GetGenericTypeDefinition() == serviceTypeDefinition
                where serviceType.IsAssignableFrom(reg.ServiceType)
                select reg)
                .ToArray();
        }
Example #3
0
        public static IEnumerable <Type> GetRegisteredInterfacesForConcreteType(this Container container, Type concreteType)
        {
            var types = container.GetCurrentRegistrations()
                        .Where(r => r.Registration.ImplementationType == concreteType)
                        .Select(r => r.ServiceType);

            var containerAsNested = container as INestedContainer;

            if (containerAsNested != null)
            {
                types = types.Union(containerAsNested.ParentContainer.GetRegisteredInterfacesForConcreteType(concreteType));
            }

            return(types);
        }
Example #4
0
        public static InstanceProducer GetInstanceProducerForServiceType(this Container container, Type serviceType)
        {
            var producer = container.GetCurrentRegistrations().FirstOrDefault(r => r.ServiceType == serviceType);

            if (producer == null)
            {
                var containerAsNested = container as INestedContainer;

                if (containerAsNested != null)
                {
                    producer = containerAsNested.ParentContainer.GetInstanceProducerForServiceType(serviceType);
                }
            }

            return(producer);
        }
        public static void AllowToResolveVariantCollections(Container container)
        {
            container.ResolveUnregisteredType += (sender, e) =>
            {
                // Only handle IEnumerable<T>.
                if (!e.UnregisteredServiceType.IsGenericType ||
                    e.UnregisteredServiceType.GetGenericTypeDefinition() !=
                        typeof(IEnumerable<>))
                {
                    return;
                }

                Type serviceType =
                    e.UnregisteredServiceType.GetGenericArguments()[0];

                if (!serviceType.IsGenericType)
                {
                    return;
                }

                Type def = serviceType.GetGenericTypeDefinition();

                var registrations = (
                    from r in container.GetCurrentRegistrations()
                    where r.ServiceType.IsGenericType
                    where r.ServiceType.GetGenericTypeDefinition() == def
                    where serviceType.IsAssignableFrom(r.ServiceType)
                    select r)
                    .ToArray();

                if (registrations.Any())
                {
                    var instances =
                        registrations.Select(r => r.GetInstance());

                    var castMethod = typeof(Enumerable).GetMethod("Cast")
                        .MakeGenericMethod(serviceType);

                    var castedInstances =
                        castMethod.Invoke(null, new[] { instances });

                    e.Register(() => castedInstances);
                }
            };
        }
 private IEnumerable <Registration> GetExistingRegistrationsFor(Type implementedInterface)
 {
     return(_container.GetCurrentRegistrations().Where(r => r.ServiceType == implementedInterface).Select(r => r.Registration));
 }
Example #7
0
 public IList <InstanceProducer> GetCurrentRegistrations()
 {
     return(_container.GetCurrentRegistrations().ToList());
 }
        private static IList<Type> GetTypes(Container container, Assembly assembly, Type[] typesToIgnore)
        {
            string assemblyName = assembly.GetName().Name;

            var existingRegistrationsServiceTypes =
                container.GetCurrentRegistrations().Select(instanceProducer => instanceProducer.ServiceType).ToList();

            IList<Type> registrations =
                assembly.GetExportedTypes()
                    .Where(type => !typesToIgnore.Contains(type))
                    .Where(type => !existingRegistrationsServiceTypes.Contains(type))
                    .Where(type => type.Namespace != null)
                    .Where(type => type.Namespace.StartsWith(assemblyName, StringComparison.OrdinalIgnoreCase))
                    .Where(type => (type.Attributes & TypeAttributes.Abstract) != TypeAttributes.Abstract)
                    .Where(type => type.GetInterfaces().Any())
                    .Where(
                        type =>
                            type.GetInterfaces()
                                .Any(
                                    inter =>
                                        !typesToIgnore.Contains(inter) && inter.Namespace != null &&
                                        inter.Namespace.StartsWith(assemblyName, StringComparison.OrdinalIgnoreCase)))

                    .ToList();

            // Ignore already registerd interfaces:
            for (int i = registrations.Count() - 1; i >= 0; i--)
            {
                foreach (var registrationInterface in registrations[i].GetInterfaces())
                {
                    if (existingRegistrationsServiceTypes.Contains(registrationInterface))
                    {
                        registrations.RemoveAt(i);
                        break;
                    }
                }
            }
            return registrations;
        }
Example #9
0
 public bool HasService(Type service)
 {
     return(_container.GetCurrentRegistrations().Any(x => x.ServiceType == service));
 }