PageRenderTime 7ms CodeModel.GetById 1ms app.highlight 3ms RepoModel.GetById 1ms app.codeStats 0ms

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