public void CanCreateSpecifyingTheServiceType()
        {
            ServiceDependencyAttribute attribute = new ServiceDependencyAttribute();

            attribute.Type = typeof(TestService);

            Assert.AreEqual(typeof(TestService), attribute.Type);
        }
        public void CanSetRequiredValue()
        {
            ServiceDependencyAttribute attribute = new ServiceDependencyAttribute();

            Assert.IsTrue(attribute.Required);

            attribute.Required = false;

            Assert.IsFalse(attribute.Required);
        }
Exemplo n.º 3
0
        protected override IDependencyResolverPolicy CreateResolver(ParameterInfo parameter)
        {
            var dependency = ServiceDependencyAttribute.GetServiceDependencies(parameter)
                             .SingleOrDefault();

            if (dependency != null)
            {
                return(new NamedTypeDependencyResolverPolicy(parameter.ParameterType, dependency.Name));
            }
            return(new NamedTypeDependencyResolverPolicy(parameter.ParameterType, null));
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="il"></param>
        /// <param name="paramAttr"></param>
        /// <param name="parameterType"></param>
        public override void EmitParameterResolution(ILGenerator il, ParameterAttribute paramAttr, Type parameterType)
        {
            ServiceDependencyAttribute attr = (ServiceDependencyAttribute)paramAttr;
            MethodInfo getLocator           = GetPropertyGetter <IBuilderContext>("Locator", typeof(IReadWriteLocator));
            MethodInfo getFromLocator       = ObtainGetFromLocatorMethod();
            MethodInfo getServices          =
                GetPropertyGetter <CompositionContainer>("Services", typeof(IServiceCollection));
            MethodInfo      getFromServices  = GetMethodInfo <IServiceCollection>("Get", typeof(Type), typeof(bool));
            ConstructorInfo newDependencyKey =
                GetConstructor <DependencyResolutionLocatorKey>(typeof(Type), typeof(string));


            // context.get_Locator
            il.Emit(OpCodes.Ldarg_0);
            il.EmitCall(OpCodes.Callvirt, getLocator, null);

            // new DependencyResolutionContainer(typeof(CompositionContainer), null)
            EmitLoadType(il, typeof(CompositionContainer));
            il.Emit(OpCodes.Ldnull);
            il.Emit(OpCodes.Newobj, newDependencyKey);
            // locator.Get(key)
            il.EmitCall(OpCodes.Callvirt, getFromLocator, null);
            il.Emit(OpCodes.Castclass, typeof(CompositionContainer));

            // container.get_Services
            il.EmitCall(OpCodes.Callvirt, getServices, null);

            if (attr.Type != null)
            {
                EmitLoadType(il, attr.Type);
            }
            else
            {
                EmitLoadType(il, parameterType);
            }

            if (attr.Required)
            {
                il.Emit(OpCodes.Ldc_I4_1);
            }
            else
            {
                il.Emit(OpCodes.Ldc_I4_0);
            }
            il.EmitCall(OpCodes.Callvirt, getFromServices, null);
        }