PageRenderTime 60ms CodeModel.GetById 20ms app.highlight 2ms RepoModel.GetById 25ms app.codeStats 1ms

/src/LinFu.AOP.Interfaces/ActivatorExtensions.cs

http://github.com/philiplaureano/LinFu
C# | 44 lines | 17 code | 2 blank | 25 comment | 0 complexity | 78f8b26f3106d9b7b9f11387699096c2 MD5 | raw file
 1using System;
 2
 3namespace LinFu.AOP.Interfaces
 4{
 5    /// <summary>
 6    ///     An extension class that adds helper methods to the <see cref="IActivator{T}" /> interface.
 7    /// </summary>
 8    public static class ActivatorExtensions
 9    {
10        /// <summary>
11        ///     Instantiates the <paramref name="targetType" /> with the given <paramref name="activator" /> and
12        ///     <paramref name="constructorArguments" />.
13        /// </summary>
14        /// <param name="activator">
15        ///     The <see cref="IActivator{T}" /> instance that will be responsible for creating the
16        ///     <paramref name="targetType" />.
17        /// </param>
18        /// <param name="targetType">The type to be created.</param>
19        /// <param name="constructorArguments">The arguments that will be passed to the constructor.</param>
20        /// <returns>An object reference that matches the given <paramref name="targetType" />.</returns>
21        public static object CreateInstance(this IActivator<IActivationContext> activator, Type targetType,
22            object[] constructorArguments)
23        {
24            var context = new ActivationContext(targetType, constructorArguments);
25            return activator.CreateInstance(context);
26        }
27
28        /// <summary>
29        ///     Instantiates the target type with the given <paramref name="activator" /> and
30        ///     <paramref name="constructorArguments" />.
31        /// </summary>
32        /// <param name="activator">
33        ///     The <see cref="IActivator{T}" /> instance that will be responsible for creating the target
34        ///     type.
35        /// </param>
36        /// <param name="constructorArguments">The arguments that will be passed to the constructor.</param>
37        /// <typeparam name="T">The target type that will be instantiated by the activator.</typeparam>
38        /// <returns>An object reference that matches the given target type.</returns>
39        public static T CreateInstance<T>(this IActivator<IActivationContext> activator, object[] constructorArguments)
40        {
41            return (T) activator.CreateInstance(typeof(T), constructorArguments);
42        }
43    }
44}