/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
- using System.Collections.Generic;
- using System.Reflection;
- namespace LinFu.IoC.Configuration
- {
- /// <summary>
- /// A class that invokes methods.
- /// </summary>
- public class MethodInvoke : BaseMethodInvoke<MethodInfo>
- {
- /// <summary>
- /// Initializes the class with the default values.
- /// </summary>
- public MethodInvoke()
- {
- MethodBuilder = new MethodBuilder();
- }
- /// <summary>
- /// Invokes the <paramref name="targetMethod" /> with the given <paramref name="arguments" />.
- /// </summary>
- /// <param name="target">The target instance.</param>
- /// <param name="originalMethod">The original method that describes the target method.</param>
- /// <param name="targetMethod">The actual method that will be invoked.</param>
- /// <param name="arguments">The method arguments.</param>
- /// <returns>The return value from the target method.</returns>
- protected override object DoInvoke(object target, MethodInfo originalMethod, MethodBase targetMethod,
- object[] arguments)
- {
- var actualArguments = new List<object>();
- // Only instance methods need a target
- if (!originalMethod.IsStatic && targetMethod.IsStatic)
- actualArguments.Add(target);
- actualArguments.AddRange(arguments);
- object result = null;
- try
- {
- result = targetMethod.Invoke(targetMethod.IsStatic ? null : target,
- actualArguments.ToArray());
- }
- catch (TargetInvocationException ex)
- {
- throw ex.InnerException;
- }
- return result;
- }
- }
- }