PageRenderTime 122ms CodeModel.GetById 80ms app.highlight 3ms RepoModel.GetById 37ms app.codeStats 1ms

/src/LinFu.Reflection/PluginLoader.cs

http://github.com/philiplaureano/LinFu
C# | 62 lines | 31 code | 6 blank | 25 comment | 3 complexity | 1ceb70c5ff50ff5b12d1177341d7a83d MD5 | raw file
 1using System;
 2using System.Collections.Generic;
 3
 4namespace LinFu.Reflection
 5{
 6    /// <summary>
 7    ///     Represents a loader class that can load
 8    ///     <see cref="ILoaderPlugin{TTarget}" /> instances
 9    ///     marked with a particular <typeparamref name="TAttribute" />
10    ///     type.
11    /// </summary>
12    /// <typeparam name="TTarget">The target type being configured.</typeparam>
13    /// <typeparam name="TAttribute">The attribute type that marks a type as a plugin type.</typeparam>
14    public class PluginLoader<TTarget, TAttribute> : BasePluginLoader<TTarget, TAttribute>
15        where TAttribute : Attribute
16    {
17        /// <summary>
18        ///     Determines if the plugin loader can load the <paramref name="inputType" />.
19        /// </summary>
20        /// <remarks>The target type must implement <see cref="ILoaderPlugin{TTarget}" /> before it can be loaded.</remarks>
21        /// <param name="inputType">The target type that might contain the target <typeparamref name="TAttribute" /> instance.</param>
22        /// <returns><c>true</c> if the type can be loaded; otherwise, it returns <c>false</c>.</returns>
23        public override bool CanLoad(Type inputType)
24        {
25            return base.CanLoad(inputType) &&
26                   typeof(ILoaderPlugin<TTarget>).IsAssignableFrom(inputType);
27        }
28
29        /// <summary>
30        ///     Loads a set of actions from a <see cref="System.Type" />
31        ///     instance.
32        /// </summary>
33        /// <param name="input">The target type to scan.</param>
34        /// <returns>A set of actions which will be applied to the target <see cref="ILoader{T}" /> instance.</returns>
35        public override IEnumerable<Action<ILoader<TTarget>>> Load(Type input)
36        {
37            // Create the plugin instance
38            var plugin = Activator.CreateInstance(input) as ILoaderPlugin<TTarget>;
39
40            if (plugin == null)
41                return new Action<ILoader<TTarget>>[0];
42
43            // Assign it to the target loader
44            Action<ILoader<TTarget>> result =
45                loader =>
46                {
47                    // If possible, initialize the plugin
48                    // with the loader
49                    if (plugin is IInitialize<ILoader<TTarget>>)
50                    {
51                        var target = plugin as IInitialize<ILoader<TTarget>>;
52                        target.Initialize(loader);
53                    }
54
55                    loader.Plugins.Add(plugin);
56                };
57
58            // Package it into an array and return the result
59            return new[] {result};
60        }
61    }
62}