PageRenderTime 525ms CodeModel.GetById 509ms RepoModel.GetById 0ms app.codeStats 0ms

/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
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Reflection;
  5. using LinFu.IoC.Configuration.Interfaces;
  6. using LinFu.IoC.Interfaces;
  7. namespace LinFu.IoC.Configuration
  8. {
  9. /// <summary>
  10. /// An <see cref="IMemberInjectionFilter{TMember}" /> implementation
  11. /// that automatically selects properties whose property types
  12. /// currently exist in the target container.
  13. /// </summary>
  14. public class PropertyInjectionFilter : BaseMemberInjectionFilter<PropertyInfo>
  15. {
  16. /// <summary>
  17. /// Determines which members should be selected from the <paramref name="targetType" />
  18. /// using the <paramref name="container" />
  19. /// </summary>
  20. /// <param name="targetType">The target type that will supply the list of members that will be filtered.</param>
  21. /// <param name="container">The target container.</param>
  22. /// <returns>A list of <see cref="PropertyInfo" /> objects that pass the filter description.</returns>
  23. protected override IEnumerable<PropertyInfo> GetMembers(Type targetType, IServiceContainer container)
  24. {
  25. var results =
  26. from p in targetType.GetProperties(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance)
  27. let propertyType = p.PropertyType
  28. let isServiceArray = propertyType.ExistsAsServiceArray()
  29. let isCompatible = isServiceArray(container) || container.Contains(propertyType)
  30. where p.CanWrite && isCompatible
  31. select p;
  32. return results;
  33. }
  34. }
  35. }