#region COPYRIGHT
//
//     THIS IS GENERATED BY TEMPLATE
//     
//     AUTHOR  :     ROYE
//     DATE       :     2010
//
//     COPYRIGHT (C) 2010, TIANXIAHOTEL TECHNOLOGIES CO., LTD. ALL RIGHTS RESERVED.
//
#endregion

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection.Emit;

namespace System.Reflection
{
    /// <summary>
    /// Extension methods for inspecting, invoking and working with methods.
    /// </summary>
    public static class MethodInfoExtensions
    {
        #region Access
        /// <summary>
        /// Invokes the static method identified by <paramref name="methodInfo"/> with <paramref name="parameters"/>
        /// as arguments.  Leave <paramref name="parameters"/> empty if the method has no argument.
        /// </summary>
        /// <returns>The return value of the method.</returns>
        /// <remarks>If the method has no return type, <c>null</c> is returned.</remarks>
        public static object Call(this MethodInfo methodInfo, params object[] parameters)
        {
            return methodInfo.DelegateForCallStaticMethod()(parameters);
        }

        /// <summary>
        /// Invokes the instance method identified by <paramref name="methodInfo"/> on the object
        /// <paramref name="obj"/> with <paramref name="parameters"/> as arguments.
        /// Leave <paramref name="parameters"/> empty if the method has no argument.
        /// </summary>
        /// <returns>The return value of the method.</returns>
        /// <remarks>If the method has no return type, <c>null</c> is returned.</remarks>
        public static object Call(this MethodInfo methodInfo, object obj, params object[] parameters)
        {
            return methodInfo.DelegateForCallMethod()(obj, parameters);
        }

        /// <summary>
        /// Creates a delegate which can invoke the static method identified by <paramref name="methodInfo"/>.
        /// </summary>
        public static StaticMethodInvoker DelegateForCallStaticMethod(this MethodInfo methodInfo)
        {
            return (StaticMethodInvoker)new MethodInvocationEmitter(methodInfo, Flags.StaticAnyVisibility).GetDelegate();
        }

        /// <summary>
        /// Creates a delegate which can invoke the instance method identified by <paramref name="methodInfo"/>.
        /// </summary>
        public static MethodInvoker DelegateForCallMethod(this MethodInfo methodInfo)
        {
            return (MethodInvoker)new MethodInvocationEmitter(methodInfo, Flags.InstanceAnyVisibility).GetDelegate();
        }
        #endregion

        #region a Parameter Lookup
        /// <summary>
        /// Gets all parameters for the given <paramref name="method"/>.
        /// </summary>
        /// <returns>The list of parameters for the method. This value will never be null.</returns>
        public static IList<ParameterInfo> Parameters(this MethodBase method)
        {
            return method.GetParameters();
        }
        #endregion
    }
}
