PageRenderTime 108ms CodeModel.GetById 105ms app.highlight 1ms RepoModel.GetById 1ms app.codeStats 1ms

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

http://github.com/philiplaureano/LinFu
C# | 37 lines | 23 code | 2 blank | 12 comment | 2 complexity | 4b92d9071cdf18f51b69318d42be00dd 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    ///     An <see cref="IMemberInjectionFilter{TMember}" /> implementation
12    ///     that automatically selects properties whose property types
13    ///     currently exist in the target container.
14    /// </summary>
15    public class PropertyInjectionFilter : BaseMemberInjectionFilter<PropertyInfo>
16    {
17        /// <summary>
18        ///     Determines which members should be selected from the <paramref name="targetType" />
19        ///     using the <paramref name="container" />
20        /// </summary>
21        /// <param name="targetType">The target type that will supply the list of members that will be filtered.</param>
22        /// <param name="container">The target container.</param>
23        /// <returns>A list of <see cref="PropertyInfo" /> objects that pass the filter description.</returns>
24        protected override IEnumerable<PropertyInfo> GetMembers(Type targetType, IServiceContainer container)
25        {
26            var results =
27                from p in targetType.GetProperties(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance)
28                let propertyType = p.PropertyType
29                let isServiceArray = propertyType.ExistsAsServiceArray()
30                let isCompatible = isServiceArray(container) || container.Contains(propertyType)
31                where p.CanWrite && isCompatible
32                select p;
33
34            return results;
35        }
36    }
37}