C# TypeInfo GetMethods(BindingFlags)

Description

TypeInfo GetMethods(BindingFlags) When overridden in a derived class, searches for the methods defined for the current Type, using the specified binding constraints.

Syntax

TypeInfo.GetMethods(BindingFlags) has the following syntax.


public abstract MethodInfo[] GetMethods(
  BindingFlags bindingAttr
)

Parameters

TypeInfo.GetMethods(BindingFlags) has the following parameters.

  • bindingAttr - A bitmask comprised of one or more BindingFlags that specify how the search is conducted.
  • bindingAttr - -or-
  • bindingAttr - Zero, to return null.

Returns

TypeInfo.GetMethods(BindingFlags) method returns

Example


using System;/* w  ww  . j av a2  s  .  c o  m*/
using System.Reflection;
using System.Reflection.Emit;

public class MyTypeClass
{
    public void MyMethods()
    {
    }
    public int MyMethods1() 
    {
        return 3;
    }
    protected String MyMethods2()
    {
        return "hello";
    }
}
public class TypeMain
{
    public static void Main() 
    {
        Type myType =(typeof(MyTypeClass));
        MethodInfo[] myArrayMethodInfo = myType.GetMethods(BindingFlags.Public|BindingFlags.Instance|BindingFlags.DeclaredOnly);
        Console.WriteLine("\nThe number of public methods is {0}.", myArrayMethodInfo.Length);

        DisplayMethodInfo(myArrayMethodInfo);

        MethodInfo[] myArrayMethodInfo1 = myType.GetMethods(BindingFlags.NonPublic|BindingFlags.Instance|BindingFlags.DeclaredOnly);
        Console.WriteLine("\nThe number of protected methods is {0}.", myArrayMethodInfo1.Length);

        DisplayMethodInfo(myArrayMethodInfo1);    
    }
    public static void DisplayMethodInfo(MethodInfo[] myArrayMethodInfo)
    {
        for(int i=0;i<myArrayMethodInfo.Length;i++)
        {
            MethodInfo myMethodInfo = (MethodInfo)myArrayMethodInfo[i];
            Console.WriteLine("\nThe name of the method is {0}.", myMethodInfo.Name);
        }
    }
}

The code above generates the following result.





















Home »
  C# Tutorial »
    System.Reflection »




EventInfo
FieldInfo
MemberInfo
MethodInfo
ParameterInfo
TypeInfo