PageRenderTime 82ms CodeModel.GetById 40ms app.highlight 3ms RepoModel.GetById 37ms app.codeStats 0ms

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

http://github.com/philiplaureano/LinFu
C# | 61 lines | 32 code | 5 blank | 24 comment | 2 complexity | dd31cea3ade8f92b466e2df59023d6e9 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 fields which have the <see cref="InjectAttribute" />
13    ///     defined.
14    /// </summary>
15    public class AttributedFieldInjectionFilter : BaseMemberInjectionFilter<FieldInfo>
16    {
17        private readonly Type _attributeType;
18
19        /// <summary>
20        ///     Initializes the class and uses the <see cref="InjectAttribute" />
21        ///     to specify which field should be automatically injected with
22        ///     services from the container.
23        /// </summary>
24        public AttributedFieldInjectionFilter()
25        {
26            _attributeType = typeof(InjectAttribute);
27        }
28
29        /// <summary>
30        ///     Initializes the class and uses the <paramref name="attributeType" />
31        ///     to specify which fields should be automatically injected with
32        ///     services from the container.
33        /// </summary>
34        /// <param name="attributeType">The custom property attribute that will be used to mark properties for automatic injection.</param>
35        public AttributedFieldInjectionFilter(Type attributeType)
36        {
37            _attributeType = attributeType;
38        }
39
40        /// <summary>
41        ///     Determines which members should be selected from the <paramref name="targetType" />
42        ///     using the <paramref name="container" />
43        /// </summary>
44        /// <param name="targetType">The target type that will supply the list of members that will be filtered.</param>
45        /// <param name="container">The target container.</param>
46        /// <returns>A list of <see cref="FieldInfo" /> objects that pass the filter description.</returns>
47        protected override IEnumerable<FieldInfo> GetMembers(Type targetType, IServiceContainer container)
48        {
49            // The field type must exist in the container and must be marked as public
50            var results =
51                from field in targetType.GetFields(BindingFlags.Public | BindingFlags.Instance)
52                let fieldType = field.FieldType
53                let attributes = field.GetCustomAttributes(_attributeType, false)
54                where attributes != null && attributes.Length > 0 &&
55                      container.Contains(fieldType)
56                select field;
57
58            return results;
59        }
60    }
61}