Type.GetMethods()方法用於獲取當前Type的方法。此方法的重載列表中有2種方法,如下所示:
- GetMethods(BindingFlags)方法
- GetMethods()方法
GetMethods(BindingFlags) Method
當在派生類中重寫時,此方法用於使用指定的綁定約束來搜索為當前Type定義的方法。
用法: 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.
返回值:此方法返回一個MethodInfo對象的數組,該對象表示為當前Type定義的,與指定的綁定約束匹配的所有方法;如果沒有為當前Type定義的方法,或者沒有定義的方法與綁定匹配,則返回MethodInfo類型的空數組約束。
以下示例程序旨在說明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() Method
此方法用於返回當前Type的所有公共方法。
用法: public System.Reflection.MethodInfo[] GetMethods ();
返回值:此方法返回一個MethodInfo對象數組,該對象表示為當前Type定義的所有公共方法;如果沒有為當前Type定義任何公共方法,則返回一個空的MethodInfo類型的數組。
以下示例程序旨在說明上述方法的用法:
示例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()
參考:
相關用法
- C# DateTimeOffset.Add()用法及代碼示例
- C# String.Contains()用法及代碼示例
- C# Math.Sin()用法及代碼示例
- C# Math.Cos()用法及代碼示例
- C# Dictionary.Add()用法及代碼示例
- C# Math.Tan()用法及代碼示例
- C# Math.Abs()方法用法及代碼示例
- C# Math.Exp()用法及代碼示例
- C# Math.Abs()函數用法及代碼示例
注:本文由純淨天空篩選整理自RohitPrasad3大神的英文原創作品 C# | Type.GetMethods() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。