PageRenderTime 72ms CodeModel.GetById 30ms app.highlight 2ms RepoModel.GetById 38ms app.codeStats 0ms

/src/LinFu.IoC/Configuration/Injectors/AutoMethodInjector.cs

http://github.com/philiplaureano/LinFu
C# | 42 lines | 19 code | 3 blank | 20 comment | 0 complexity | 6b16e8b88b70396d51a1c79bf07142fb MD5 | raw file
 1using System.Linq;
 2using System.Reflection;
 3using LinFu.IoC.Configuration.Interfaces;
 4using LinFu.IoC.Interfaces;
 5
 6namespace LinFu.IoC.Configuration
 7{
 8    /// <summary>
 9    ///     A class that automatically invokes methods using arguments
10    ///     derived from existing instances from within a <see cref="IServiceContainer" />
11    ///     instance.
12    /// </summary>
13    public class AutoMethodInjector : AutoMemberInjector<MethodInfo>
14    {
15        /// <summary>
16        ///     Injects services from the <paramref name="container" /> into the target <see cref="MethodInfo" /> instance.
17        /// </summary>
18        /// <param name="target">The target object.</param>
19        /// <param name="method">The <see cref="MethodInfo" /> instance that will store the service instance.</param>
20        /// <param name="resolver">
21        ///     The <see cref="IArgumentResolver" /> that will determine which arguments will be assigned to the
22        ///     target member.
23        /// </param>
24        /// <param name="additionalArguments">
25        ///     The additional arguments that were passed to the <see cref="IServiceRequestResult" />
26        ///     during the instantiation process.
27        /// </param>
28        /// <param name="container">The container that will provide the service instances.</param>
29        protected override void Inject(object target, MethodInfo method,
30            IArgumentResolver resolver, IServiceContainer container,
31            object[] additionalArguments)
32        {
33            var parameterTypes = from p in method.GetParameters()
34                select new NamedType(p) as INamedType;
35
36            var arguments = resolver.ResolveFrom(parameterTypes, container, additionalArguments);
37
38            // Invoke the target method
39            method.Invoke(target, arguments);
40        }
41    }
42}