PageRenderTime 254ms CodeModel.GetById 239ms RepoModel.GetById 0ms app.codeStats 0ms

/src/LinFu.Reflection/CollectionExtensions.cs

http://github.com/philiplaureano/LinFu
C# | 50 lines | 28 code | 4 blank | 18 comment | 2 complexity | 61e8433f4d70681707c39fac9ee69f60 MD5 | raw file
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. namespace LinFu.Reflection
  5. {
  6. /// <summary>
  7. /// Adds additional support methods to the standard System.Collection classes.
  8. /// </summary>
  9. public static class CollectionExtensions
  10. {
  11. /// <summary>
  12. /// Determines whether or not an element exists that matches the given
  13. /// <paramref name="predicate" />.
  14. /// </summary>
  15. /// <typeparam name="T">The element type.</typeparam>
  16. /// <param name="items">The list of items being searched.</param>
  17. /// <param name="predicate">The predicate that will be used to describe the matching items.</param>
  18. /// <returns>Returns <c>true</c> if at least one match is found; otherwise, it will return <c>false</c>.</returns>
  19. public static bool HasElementWith<T>(this IEnumerable<T> items, Func<T, bool> predicate)
  20. {
  21. var matches = from item in items
  22. where predicate(item)
  23. select item;
  24. return matches.Count() > 0;
  25. }
  26. /// <summary>
  27. /// Loads a list of types that match the given <typeparamref name="T">target type</typeparamref>.
  28. /// </summary>
  29. /// <typeparam name="T">The target type to be loaded.</typeparam>
  30. /// <param name="list">The list that will hold the instances of the target type.</param>
  31. /// <param name="targetDirectory">The directory that will be used to scan for assemblies that contain the target type.</param>
  32. /// <param name="filespec">The wildcard pattern that describes the files to be loaded.</param>
  33. public static void LoadFrom<T>(this ICollection<T> list, string targetDirectory, string filespec)
  34. where T : class
  35. {
  36. if (list == null)
  37. throw new ArgumentNullException("list");
  38. var loader = new Loader<ICollection<T>>();
  39. var targetLoader = new AssemblyTargetLoader<ICollection<T>>();
  40. targetLoader.TypeLoaders.Add(new CollectionLoader<T>());
  41. loader.FileLoaders.Add(targetLoader);
  42. loader.LoadDirectory(targetDirectory, filespec);
  43. loader.LoadInto(list);
  44. }
  45. }
  46. }