Searches for the specified public method whose parameters match the specified argument types. - CSharp System.Reflection

CSharp examples for System.Reflection:Type

Description

Searches for the specified public method whose parameters match the specified argument types.

Demo Code

// See LICENSE.txt for details or visit http://www.opensource.org/licenses/ms-pl.html
using System.Reflection;
using System.Linq;
using System;/*w  w  w. java 2  s .c om*/

public class Main{
        /// <summary>
      /// Searches for the specified public method whose parameters match the specified argument types.
      /// </summary>
      /// <param name="type">The type.</param>
      /// <param name="name">The System.String containing the name of the public method to get.</param>
      /// <param name="types">An array of System.Type objects representing the number, order, and type of the parameters for the method to get.-or- An empty array of System.Type objects (as provided by the System.Type.EmptyTypes field) to get a method that takes no parameters.</param>
      /// <returns></returns>
      public static MethodInfo GetMethod( this Type type, string name, Type[] types )
      {
         return ( from method in type.GetRuntimeMethods()
                  where method.Name == name
                where method.GetParameters().Select( p => p.ParameterType ).SequenceEqual( types )
                  select method ).SingleOrDefault();
      }
        /// <summary>
      /// Searches for the public method with the specified name.
      /// </summary>
      /// <param name="type">The type.</param>
      /// <param name="name">The System.String containing the name of the public method to get.</param>
      /// <returns>A System.Reflection.MethodInfo object representing the public method with the specified name, if found; otherwise, null.</returns>
      public static MethodInfo GetMethod( this Type type, string name )
      {
         return ( from method in type.GetRuntimeMethods()
                  where method.Name == name
                select method ).SingleOrDefault();
      }
}

Related Tutorials