/src/LinFu.IoC/Configuration/MethodInvoke.cs

http://github.com/philiplaureano/LinFu · C# · 51 lines · 31 code · 5 blank · 15 comment · 2 complexity · efb2ef59d477bab048c25debac515b30 MD5 · raw file

  1. using System.Collections.Generic;
  2. using System.Reflection;
  3. namespace LinFu.IoC.Configuration
  4. {
  5. /// <summary>
  6. /// A class that invokes methods.
  7. /// </summary>
  8. public class MethodInvoke : BaseMethodInvoke<MethodInfo>
  9. {
  10. /// <summary>
  11. /// Initializes the class with the default values.
  12. /// </summary>
  13. public MethodInvoke()
  14. {
  15. MethodBuilder = new MethodBuilder();
  16. }
  17. /// <summary>
  18. /// Invokes the <paramref name="targetMethod" /> with the given <paramref name="arguments" />.
  19. /// </summary>
  20. /// <param name="target">The target instance.</param>
  21. /// <param name="originalMethod">The original method that describes the target method.</param>
  22. /// <param name="targetMethod">The actual method that will be invoked.</param>
  23. /// <param name="arguments">The method arguments.</param>
  24. /// <returns>The return value from the target method.</returns>
  25. protected override object DoInvoke(object target, MethodInfo originalMethod, MethodBase targetMethod,
  26. object[] arguments)
  27. {
  28. var actualArguments = new List<object>();
  29. // Only instance methods need a target
  30. if (!originalMethod.IsStatic && targetMethod.IsStatic)
  31. actualArguments.Add(target);
  32. actualArguments.AddRange(arguments);
  33. object result = null;
  34. try
  35. {
  36. result = targetMethod.Invoke(targetMethod.IsStatic ? null : target,
  37. actualArguments.ToArray());
  38. }
  39. catch (TargetInvocationException ex)
  40. {
  41. throw ex.InnerException;
  42. }
  43. return result;
  44. }
  45. }
  46. }