Get methods using the specified binding constraints in CSharp
Description
The following code shows how to get methods using the specified binding constraints.
Example
using System;/*from w ww . j a va 2s .com*/
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.