Beispiel #1
0
        public ComponentRegistration RegistrationFor(Service service)
        {
            var swt = service as IServiceWithType;

            if (swt == null)
            {
                return(null);
            }

            Type resolutionType = swt.ServiceType;

            if (!resolutionType.IsGenericType || !resolutionType.IsConstructedGenericType)
            {
                return(null);
            }

            IServiceWithType openGenericService = swt.ChangeType(resolutionType.GetGenericTypeDefinition());

            if (!openGenericService.Equals(genericService))
            {
                return(null);
            }

            Type constructedImplementorType =
                implementorType.MakeGenericType(resolutionType.GenericTypeArguments);

            return(new ComponentRegistration(
                       service,
                       new ReflectiveActivator(constructedImplementorType)));
        }
Beispiel #2
0
        public ComponentRegistration RegistrationFor(Service service)
        {
            #region Please implement the method to pass the test

            /*
             * This source has 2 properties: the genericService is used as the key to match the
             * service argument. And the implementorType is used to record the actual type used
             * to create instances.
             *
             * This method will try matching the constructed service to an non-constructed
             * generic type of genericService. If it is matched, then an concrete component
             * registration needed wll be invoked.
             */

            var serviceWithType = service as IServiceWithType;
            if (serviceWithType == null)
            {
                return(null);
            }

            var resolutionType = serviceWithType.ServiceType;
            if (!resolutionType.IsConstructedGenericType)
            {
                return(null);
            }

            var genericDefinitionType = serviceWithType.ChangeType(resolutionType.GetGenericTypeDefinition());
            if (!genericService.Equals(genericDefinitionType))
            {
                return(null);
            }

            var genericArguments = resolutionType.GetGenericArguments();
            return(new ComponentRegistration(service,
                                             new ReflectiveActivator(implementorType.MakeGenericType(genericArguments))));

            #endregion
        }