PageRenderTime 223ms CodeModel.GetById 121ms app.highlight 3ms RepoModel.GetById 98ms app.codeStats 0ms

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

http://github.com/philiplaureano/LinFu
C# | 58 lines | 33 code | 5 blank | 20 comment | 3 complexity | 93dac1a1a0b71b3432b2fb7c36abc2bd MD5 | raw file
 1using System;
 2using System.Collections.Generic;
 3using System.Linq;
 4using System.Reflection;
 5using LinFu.IoC.Configuration.Interfaces;
 6using LinFu.IoC.Interfaces;
 7
 8namespace LinFu.IoC.Configuration
 9{
10    /// <summary>
11    ///     A default implementation of the <see cref="IMemberInjectionFilter{TMember}" />
12    ///     class that returns methods which have the <see cref="InjectAttribute" />
13    ///     defined.
14    /// </summary>
15    public class AttributedMethodInjectionFilter : BaseMemberInjectionFilter<MethodInfo>
16    {
17        private readonly Type _attributeType;
18
19        /// <summary>
20        ///     Initializes the class with the <see cref="InjectAttribute" /> as the
21        ///     default injection attribute.
22        /// </summary>
23        public AttributedMethodInjectionFilter()
24        {
25            _attributeType = typeof(InjectAttribute);
26        }
27
28        /// <summary>
29        ///     Initializes the class and uses the <paramref name="attributeType" />
30        ///     as the custom injection attribute.
31        /// </summary>
32        /// <param name="attributeType"></param>
33        public AttributedMethodInjectionFilter(Type attributeType)
34        {
35            _attributeType = attributeType;
36        }
37
38        /// <summary>
39        ///     Returns the methods that have the custom attribute type defined.
40        /// </summary>
41        /// <param name="targetType">The target type that contains the target methods.</param>
42        /// <param name="container">The host container.</param>
43        /// <returns>The list of methods that have the custom attribute type defined.</returns>
44        protected override IEnumerable<MethodInfo> GetMembers(Type targetType,
45            IServiceContainer container)
46        {
47            var results =
48                from method in targetType.GetMethods(BindingFlags.Public | BindingFlags.Instance)
49                let attributes = _attributeType != null
50                    ? method.GetCustomAttributes(_attributeType, false)
51                    : null
52                where attributes != null && attributes.Length > 0
53                select method;
54
55            return results;
56        }
57    }
58}