Type.GetMethods()方法用于获取当前Type的方法。此方法的重载列表中有2种方法,如下所示:
- GetMethods(BindingFlags)方法
- GetMethods()方法
GetMethods(BindingFlags)方法
当在派生类中重写时,此方法用于使用指定的绑定约束来搜索为当前Type定义的方法。
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.
下面的程序说明了Type.GetMethods(BindingFlags)方法的用法:
范例1:
// C# program to demonstrate the
// Type.GetMethods() Method
using System;
using System.Globalization;
using System.Reflection;
// Defining class Empty
public class Empty { }
class GFG {
// Main Method
public static void Main()
{
// Declaring and initializing object of Type
Type objType = typeof(Empty);
// try-catch block for handling Exception
try {
// Getting array of Method by
// using GetMethods() Method
MethodInfo[] info = objType.GetMethods(BindingFlags.Public | BindingFlags.Instance);
// Display the Result
Console.WriteLine("Methods of current type is as Follow: ");
for (int i = 0; i < info.Length; i++)
Console.WriteLine(" {0}", info[i]);
}
// catch ArgumentNullException here
catch (ArgumentNullException e)
{
Console.Write("name is null.");
Console.Write("Exception Thrown: ");
Console.Write("{0}", e.GetType(), e.Message);
}
}
}
Methods of current type is as Follow:
Boolean Equals(System.Object)
Int32 GetHashCode()
System.Type GetType()
System.String ToString()
范例2:
// C# program to demonstrate the
// Type.GetMethods() Method
using System;
using System.Globalization;
using System.Reflection;
class GFG {
// Main Method
public static void Main()
{
// Declaring and initializing object of Type
Type objType = typeof(int);
// try-catch block for handling Exception
try {
// Getting array of Method by
// using GetMethods() Method
MethodInfo[] info = objType.GetMethods(BindingFlags.Public | BindingFlags.Static);
// Display the Result
Console.WriteLine("Methods of current type is as Follow: ");
for (int i = 0; i < info.Length; i++)
Console.WriteLine(" {0}", info[i]);
}
// catch ArgumentNullException here
catch (ArgumentNullException e)
{
Console.Write("name is null.");
Console.Write("Exception Thrown: ");
Console.Write("{0}", e.GetType(), e.Message);
}
}
}
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()方法
此方法用于返回当前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.
下面的程序说明了上述方法的用法:
范例1:
// C# program to demonstrate the
// Type.GetMethods() Method
using System;
using System.Globalization;
using System.Reflection;
// Defining class Empty
class Empty { }
class GFG {
// Main Method
public static void Main()
{
// Declaring and initializing object of Type
Type objType = typeof(Empty);
// try-catch block for handling Exception
try {
// Getting array of Method by
// using GetMethods() Method
MethodInfo[] info = objType.GetMethods();
// Display the Result
Console.WriteLine("Methods of current type is as Follow: ");
for (int i = 0; i < info.Length; i++)
Console.WriteLine(" {0}", info[i]);
}
// catch ArgumentNullException here
catch (ArgumentNullException e)
{
Console.Write("name is null.");
Console.Write("Exception Thrown: ");
Console.Write("{0}", e.GetType(), e.Message);
}
}
}
Methods of current type is as Follow:
Boolean Equals(System.Object)
Int32 GetHashCode()
System.Type GetType()
System.String ToString()
范例2:
// C# program to demonstrate the
// Type.GetMethods() Method
using System;
using System.Globalization;
using System.Reflection;
// Defining class Student
public class Student {
private string name, dept;
private int roll;
// Constructor
public Student(string name, int roll, string dept)
{
this.name = name;
this.roll = roll;
this.dept = dept;
}
// getter for name
public string getName()
{
return name;
}
// getter for roll
public int getRoll()
{
return roll;
}
// getter for dept
public string getDept()
{
return dept;
}
}
class GFG {
// Main Method
public static void Main()
{
// Declaring and initializing object of Type
Type objType = typeof(Student);
// try-catch block for handling Exception
try {
// Getting array of Method by
// using GetMethods() Method
MethodInfo[] info = objType.GetMethods(BindingFlags.Public | BindingFlags.Instance);
// Display the Result
Console.WriteLine("Methods of current type is as Follow: ");
for (int i = 0; i < info.Length; i++)
Console.WriteLine(" {0}", info[i]);
}
// catch ArgumentNullException here
catch (ArgumentNullException e)
{
Console.Write("name is null.");
Console.Write("Exception Thrown: ");
Console.Write("{0}", e.GetType(), e.Message);
}
}
}
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()
参考:
- https://docs.microsoft.com/zh-cn/dotnet/api/system.type.getmethods?view=netframework-4.8