/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
- using System;
- namespace LinFu.AOP.Interfaces
- {
- /// <summary>
- /// An extension class that adds helper methods to the <see cref="IActivator{T}" /> interface.
- /// </summary>
- public static class ActivatorExtensions
- {
- /// <summary>
- /// Instantiates the <paramref name="targetType" /> with the given <paramref name="activator" /> and
- /// <paramref name="constructorArguments" />.
- /// </summary>
- /// <param name="activator">
- /// The <see cref="IActivator{T}" /> instance that will be responsible for creating the
- /// <paramref name="targetType" />.
- /// </param>
- /// <param name="targetType">The type to be created.</param>
- /// <param name="constructorArguments">The arguments that will be passed to the constructor.</param>
- /// <returns>An object reference that matches the given <paramref name="targetType" />.</returns>
- public static object CreateInstance(this IActivator<IActivationContext> activator, Type targetType,
- object[] constructorArguments)
- {
- var context = new ActivationContext(targetType, constructorArguments);
- return activator.CreateInstance(context);
- }
- /// <summary>
- /// Instantiates the target type with the given <paramref name="activator" /> and
- /// <paramref name="constructorArguments" />.
- /// </summary>
- /// <param name="activator">
- /// The <see cref="IActivator{T}" /> instance that will be responsible for creating the target
- /// type.
- /// </param>
- /// <param name="constructorArguments">The arguments that will be passed to the constructor.</param>
- /// <typeparam name="T">The target type that will be instantiated by the activator.</typeparam>
- /// <returns>An object reference that matches the given target type.</returns>
- public static T CreateInstance<T>(this IActivator<IActivationContext> activator, object[] constructorArguments)
- {
- return (T) activator.CreateInstance(typeof(T), constructorArguments);
- }
- }
- }