/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

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