Пример #1
0
        public IInstanceFactory Handle(IResolverRequest request, Func<IResolverRequest, IInstanceFactory> nextHandler)
        {
            if (request == null) throw new ArgumentNullException("request");
            if (nextHandler == null) throw new ArgumentNullException("nextHandler");

            // If the component handler is last in the pipeline, this will return null.
            // However, it might not *always* be the last handler, so stick to the pattern
            // and pass to the next handler rather than returning null explicitly.
            if (request.RequestType.IsAbstract) return nextHandler(request);

            var ctors = request.RequestType
                .GetConstructors(BindingFlags.Public | BindingFlags.DeclaredOnly | BindingFlags.Instance);

            // this bad boy does the actual resolving...
            var ctorResolver = new ConstructorResolver(_pipeline);

            // ... we just order the results
            var factory = ctors
                .Select(ctor => ctorResolver.TryResolve(request, ctor))
                .Where(f => f != null) // remove unresolvable ctors
                .OrderByDescending(f => f.Resolvable) // order by most parameters
                .OrderBy(f => f.Optional) // with the least optional parameters
                .FirstOrDefault();

            return factory;
        }
        public TDelegate ExecuteConstructorLambda <TDelegate>(ConstructorInfo ctor) where TDelegate : class
        {
            var resolver = new ConstructorResolver(ctor);

            return(Lambda.ResolveConstructorTo <TDelegate>(resolver)
                   .ToLambda()
                   .Compile());
        }
Пример #3
0
        public void ShouldSelectConstructorWithMostResolvableParameters()
        {
            var targetType = typeof(Vehicle);
            var constructorImplementations = (from c in typeof(Vehicle).GetConstructors()
                                              select new ConstructorCall(c) as IImplementation <ConstructorInfo>).AsEnumerable();

            IImplementation <ConstructorInfo> expectedImplementation = GetExpectedConstructorImplementation(constructorImplementations);

            var map = new Mock <IDependencyContainer>();

            map.Expect(m => m.Contains(It.IsAny <Dependency>())).Returns(true);
            map.Expect(m => m.Dependencies).Returns(new IDependency[] {});

            var constructorResolver = new ConstructorResolver();
            IImplementation <ConstructorInfo> result = constructorResolver.ResolveFrom(targetType, map.Object);

            Assert.AreEqual(expectedImplementation.Target, result.Target);

            map.VerifyAll();
        }
Пример #4
0
        public void CanApplyConstructorArgsToAbstractType()
        {
            IResource        resource = new ReadOnlyXmlTestResource("ctor-args.xml", GetType());
            XmlObjectFactory xof      = new XmlObjectFactory(resource);
            TestObject       rod      = (TestObject)xof.GetObject("rod");

            Assert.AreEqual(1, rod.Age);

            RootObjectDefinition def      = (RootObjectDefinition)xof.GetObjectDefinition("rod");
            ConstructorResolver  resolver = new ConstructorResolver(xof, xof, new SimpleInstantiationStrategy(),
                                                                    new ObjectDefinitionValueResolver(xof));

            ConstructorInstantiationInfo ci = resolver.GetConstructorInstantiationInfo("rod", def, null, null);

            AbstractObjectDefinition objDef = (AbstractObjectDefinition)xof.GetObjectDefinition("foo");

            objDef.IsAbstract = false;

            TestObject foo = (TestObject)xof.GetObject("foo");


            Assert.AreEqual(2, foo.Age);
        }
Пример #5
0
            /// <summary>
            /// Applies attributes to the proxy class.
            /// </summary>
            /// <param name="typeBuilder">The type builder to use.</param>
            /// <param name="targetType">The proxied class.</param>
            /// <see cref="IProxyTypeBuilder.ProxyTargetAttributes"/>
            /// <see cref="IProxyTypeBuilder.TypeAttributes"/>
            protected override void ApplyTypeAttributes(TypeBuilder typeBuilder, Type targetType)
            {
                foreach (object attr in GetTypeAttributes(targetType))
                {
                    if (attr is CustomAttributeBuilder)
                    {
                        typeBuilder.SetCustomAttribute((CustomAttributeBuilder)attr);
                    }
                    else if (attr is CustomAttributeData)
                    {
                        typeBuilder.SetCustomAttribute(
                            ReflectionUtils.CreateCustomAttribute((CustomAttributeData)attr));
                    }
                    else if (attr is Attribute)
                    {
                        typeBuilder.SetCustomAttribute(
                            ReflectionUtils.CreateCustomAttribute((Attribute)attr));
                    }
                    else if (attr is IObjectDefinition)
                    {
                        RootObjectDefinition objectDefinition = (RootObjectDefinition)attr;

                        //TODO check that object definition is for an Attribute type.

                        //Change object definition so it can be instantiated and make prototype scope.
                        objectDefinition.IsAbstract  = false;
                        objectDefinition.IsSingleton = false;
                        string objectName = ObjectDefinitionReaderUtils.GenerateObjectName(objectDefinition, objectFactory);
                        objectFactory.RegisterObjectDefinition(objectName, objectDefinition);


                        //find constructor and constructor arg values to create this attribute.
                        ConstructorResolver constructorResolver = new ConstructorResolver(objectFactory, objectFactory,
                                                                                          new SimpleInstantiationStrategy(),
                                                                                          new ObjectDefinitionValueResolver(objectFactory));


                        ConstructorInstantiationInfo ci = constructorResolver.GetConstructorInstantiationInfo(objectName,
                                                                                                              objectDefinition,
                                                                                                              null, null);

                        if (objectDefinition.PropertyValues.PropertyValues.Count == 0)
                        {
                            CustomAttributeBuilder cab = new CustomAttributeBuilder(ci.ConstructorInfo,
                                                                                    ci.ArgInstances);
                            typeBuilder.SetCustomAttribute(cab);
                        }
                        else
                        {
                            object         attributeInstance        = objectFactory.GetObject(objectName);
                            IObjectWrapper wrappedAttributeInstance = new ObjectWrapper(attributeInstance);
                            PropertyInfo[] namedProperties          = wrappedAttributeInstance.GetPropertyInfos();
                            object[]       propertyValues           = new object[namedProperties.Length];
                            for (int i = 0; i < namedProperties.Length; i++)
                            {
                                propertyValues[i] =
                                    wrappedAttributeInstance.GetPropertyValue(namedProperties[i].Name);
                            }
                            CustomAttributeBuilder cab = new CustomAttributeBuilder(ci.ConstructorInfo, ci.ArgInstances,
                                                                                    namedProperties, propertyValues);
                            typeBuilder.SetCustomAttribute(cab);
                        }
                    }
                }
            }