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