Represents the data associated with a IMethodFinder{T} search.
Inheritance: IMethodFinderContext
        /// <summary>
        /// Creates an object instance.
        /// </summary>
        /// <returns>A valid object instance.</returns>
        public object CreateInstance(IContainerActivationContext context)
        {
            var container           = context.Container;
            var additionalArguments = context.AdditionalArguments;
            var concreteType        = context.TargetType;

            // Add the required services if necessary
            container.AddDefaultServices();

            var finderContext = new MethodFinderContext(new Type[0], additionalArguments, null);

            // Determine which constructor
            // contains the most resolvable
            // parameters
            var constructor = _resolver.ResolveFrom(concreteType, container, finderContext);

            // TODO: Allow users to insert their own custom constructor resolution routines here
            var parameterTypes = GetMissingParameterTypes(constructor, finderContext.Arguments);

            // Generate the arguments for the target constructor
            var arguments = _argumentResolver.ResolveFrom(parameterTypes, container,
                                                          additionalArguments);
            // Instantiate the object
            var result = _constructorInvoke.Invoke(null, constructor, arguments);

            return(result);
        }
Exemple #2
0
        public void ShouldFindGenericMethod()
        {
            var container = new ServiceContainer();
            container.LoadFromBaseDirectory("*.dll");

            var context = new MethodFinderContext(new Type[]{typeof(object)}, new object[0], typeof(void));
            var methods = typeof(SampleClassWithGenericMethod).GetMethods(BindingFlags.Public | BindingFlags.Instance);
            var finder = container.GetService<IMethodFinder<MethodInfo>>();
            var result = finder.GetBestMatch(methods, context);

            Assert.IsTrue(result.IsGenericMethod);
            Assert.IsTrue(result.GetGenericArguments().Count() == 1);
        }
        public void ShouldResolveConstructorWithMostResolvableParametersFromContainer()
        {
            var mockSampleService = new Mock<ISampleService>();
            IServiceContainer container = new ServiceContainer();

            // Add an ISampleService instance
            container.AddService(mockSampleService.Object);
            container.AddDefaultServices();
            var resolver = container.GetService<IMemberResolver<ConstructorInfo>>();
            Assert.IsNotNull(resolver);

            // The resolver should return the constructor with two ISampleService parameters
            var expectedConstructor =
                typeof (SampleClassWithMultipleConstructors).GetConstructor(new[]
                {
                    typeof (ISampleService),
                    typeof (ISampleService)
                });
            Assert.IsNotNull(expectedConstructor);

            var finderContext = new MethodFinderContext(new Type[0], new object[0], null);
            var result = resolver.ResolveFrom(typeof (SampleClassWithMultipleConstructors), container,
                finderContext);
            Assert.AreSame(expectedConstructor, result);
        }
        public void ShouldResolveConstructorWithAdditionalArgument()
        {
            var mockSampleService = new Mock<ISampleService>();
            IServiceContainer container = new ServiceContainer();

            // Add an ISampleService instance
            container.AddService(mockSampleService.Object);
            container.AddDefaultServices();

            var resolver = container.GetService<IMemberResolver<ConstructorInfo>>();
            Assert.IsNotNull(resolver);

            // The resolver should return the constructor
            // with the following signature: Constructor(ISampleService, int)
            var expectedConstructor =
                typeof (SampleClassWithAdditionalArgument).GetConstructor(new[] {typeof (ISampleService), typeof (int)});
            Assert.IsNotNull(expectedConstructor);

            var context = new MethodFinderContext(42);
            var result = resolver.ResolveFrom(typeof (SampleClassWithAdditionalArgument), container, context);
            Assert.AreSame(expectedConstructor, result);
        }