C# | Type.GetMethods() Method
Type.GetMethods() Method is used to get the methods of the current Type. There are 2 methods in the overload list of this method as follows:
- GetMethods(BindingFlags) Method
- GetMethods() Method
GetMethods(BindingFlags) Method
This method is used to search for the methods defined for the current Type, using the specified binding constraints when overridden in a derived class.
Syntax: public abstract System.Reflection.MethodInfo[] GetMethods (System.Reflection.BindingFlags bindingAttr);
Here, it takes a bitmask comprised of one or more BindingFlags which specify how the search is conducted or,
Zero (Default), to return an empty array.
Return Value: This method returns an array of MethodInfo objects representing all methods defined for the current Type which match the specified binding constraints Or an empty array of type MethodInfo, if no methods are defined for the current Type, or if none of the defined methods match the binding constraints.
Below programs illustrate the use of Type.GetMethods(BindingFlags) Method:
Example 1:
using System;
using System.Globalization;
using System.Reflection;
public class Empty { }
class GFG {
public static void Main()
{
Type objType = typeof (Empty);
try {
MethodInfo[] info = objType.GetMethods(BindingFlags.Public | BindingFlags.Instance);
Console.WriteLine( "Methods of current type is as Follow: " );
for ( int i = 0; i < info.Length; i++)
Console.WriteLine( " {0}" , info[i]);
}
catch (ArgumentNullException e)
{
Console.Write( "name is null." );
Console.Write( "Exception Thrown: " );
Console.Write( "{0}" , e.GetType(), e.Message);
}
}
}
|
Output:
Methods of current type is as Follow:
Boolean Equals(System.Object)
Int32 GetHashCode()
System.Type GetType()
System.String ToString()
Example 2:
using System;
using System.Globalization;
using System.Reflection;
class GFG {
public static void Main()
{
Type objType = typeof ( int );
try {
MethodInfo[] info = objType.GetMethods(BindingFlags.Public | BindingFlags.Static);
Console.WriteLine( "Methods of current type is as Follow: " );
for ( int i = 0; i < info.Length; i++)
Console.WriteLine( " {0}" , info[i]);
}
catch (ArgumentNullException e)
{
Console.Write( "name is null." );
Console.Write( "Exception Thrown: " );
Console.Write( "{0}" , e.GetType(), e.Message);
}
}
}
|
Output:
Methods of current type is as Follow:
Int32 Parse(System.String)
Int32 Parse(System.String, System.Globalization.NumberStyles)
Int32 Parse(System.String, System.IFormatProvider)
Int32 Parse(System.String, System.Globalization.NumberStyles, System.IFormatProvider)
Boolean TryParse(System.String, Int32 ByRef)
Boolean TryParse(System.String, System.Globalization.NumberStyles, System.IFormatProvider, Int32 ByRef)
GetMethods() Method
This method is used to return all the public methods of the current Type.
Syntax: public System.Reflection.MethodInfo[] GetMethods ();
Return Value: This method returns an array of MethodInfo objects representing all the public methods defined for the current Type or an empty array of type MethodInfo if no public methods are defined for the current Type.
Below programs illustrate the use of the above-discussed method:
Example 1:
using System;
using System.Globalization;
using System.Reflection;
class Empty { }
class GFG {
public static void Main()
{
Type objType = typeof (Empty);
try {
MethodInfo[] info = objType.GetMethods();
Console.WriteLine( "Methods of current type is as Follow: " );
for ( int i = 0; i < info.Length; i++)
Console.WriteLine( " {0}" , info[i]);
}
catch (ArgumentNullException e)
{
Console.Write( "name is null." );
Console.Write( "Exception Thrown: " );
Console.Write( "{0}" , e.GetType(), e.Message);
}
}
}
|
Output:
Methods of current type is as Follow:
Boolean Equals(System.Object)
Int32 GetHashCode()
System.Type GetType()
System.String ToString()
Example 2:
using System;
using System.Globalization;
using System.Reflection;
public class Student {
private string name, dept;
private int roll;
public Student( string name, int roll, string dept)
{
this .name = name;
this .roll = roll;
this .dept = dept;
}
public string getName()
{
return name;
}
public int getRoll()
{
return roll;
}
public string getDept()
{
return dept;
}
}
class GFG {
public static void Main()
{
Type objType = typeof (Student);
try {
MethodInfo[] info = objType.GetMethods(BindingFlags.Public | BindingFlags.Instance);
Console.WriteLine( "Methods of current type is as Follow: " );
for ( int i = 0; i < info.Length; i++)
Console.WriteLine( " {0}" , info[i]);
}
catch (ArgumentNullException e)
{
Console.Write( "name is null." );
Console.Write( "Exception Thrown: " );
Console.Write( "{0}" , e.GetType(), e.Message);
}
}
}
|
Output:
Methods of current type is as Follow:
System.String getName()
Int32 getRoll()
System.String getDept()
Boolean Equals(System.Object)
Int32 GetHashCode()
System.Type GetType()
System.String ToString()
Reference:
Last Updated :
16 Dec, 2019
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...