Type.GetMember()方法用於獲取當前Type的指定成員。此方法的重載列表中有3種方法,如下所示:
- GetMember(String)方法
- GetMember(String,BindingFlags)方法
- GetMember(String,MemberTypes,BindingFlags)方法
GetMember(String) Method
此方法搜索具有指定名稱的公共成員。
用法: public System.Reflection.MemberInfo[] GetMember (string name);
Here, it takes the string containing the name of the public members to get.
返回值:此方法返回一個MemberInfo對象數組,該數組代表具有指定名稱的公共成員,如果找不到,則為空數組。
異常:如果name為null,則此方法引發ArgumentNullException。
以下示例程序旨在說明上述方法的用法:
示例1:
// C# program to demonstrate the
// Type.GetMember(String) 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(Student);
// try-catch block for handling Exception
try {
MemberInfo[] info = objType.GetMember("Name");
// Display the Result
Console.WriteLine("Members 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);
}
}
}
// Defining class Student
public class Student
{
public string Name = "Rahul";
public string Dept = "Electrical";
public int Roll = 10;
public static int id = 02;
}
Members of current type is as Follow: System.String Name
示例2:對於ArgumentNullException
// C# program to demonstrate the
// Type.GetMember(String) 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(Student);
// try-catch block for handling Exception
try {
MemberInfo[] info = objType.GetMember(null);
// Display the Result
Console.WriteLine("Members 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.WriteLine("name is null.");
Console.Write("Exception Thrown: ");
Console.Write("{0}", e.GetType(), e.Message);
}
}
}
// Defining class Student
public class Student
{
public string Name = "Rahul";
public string Dept = "Electrical";
public int Roll = 10;
public static int id = 02;
}
name is null. Exception Thrown: System.ArgumentNullException
GetMember(String, BindingFlags) Method
此方法用於使用指定的綁定約束來搜索指定的成員。
用法: public virtual System.Reflection.MemberInfo[] GetMember (string name, System.Reflection.BindingFlags bindingAttr);
參數:
name:它是包含要獲取的成員名稱的字符串。
bindingAttr:這是一個由一個或多個BindingFlags組成的位掩碼,用於指定如何進行搜索。或零,返回一個空數組。
返回值:如果發現否則,此方法返回一個MemberInfo對象數組,該數組代表具有指定名稱的公共成員,否則為空數組。
異常:如果name為null,則此方法將引發ArgumentNullException。
以下示例程序旨在說明上述方法的用法:
示例1:
// C# program to demonstrate the
// Type.GetMember(String, BindingFlags)
// 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(Student);
// try-catch block for handling Exception
try {
MemberInfo[] info = objType.GetMember("Name",
BindingFlags.Public | BindingFlags.Instance);
// Display the Result
Console.WriteLine("Members 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.WriteLine("name is null.");
Console.Write("Exception Thrown: ");
Console.Write("{0}", e.GetType(), e.Message);
}
}
}
// Defining class Student
public class Student
{
public string Name = "Rahul";
public string Dept = "Electrical";
public int Roll = 10;
public static int id = 02;
}
Members of current type is as Follow: System.String Name
示例2:對於ArgumentNullException
// C# program to demonstrate the
// Type.GetMember(String, BindingFlags) 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(Student);
// try-catch block for handling Exception
try {
MemberInfo[] info = objType.GetMember(null,
BindingFlags.Public | BindingFlags.Instance);
// Display the Result
Console.WriteLine("Members 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.WriteLine("name is null.");
Console.Write("Exception Thrown: ");
Console.Write("{0}", e.GetType(), e.Message);
}
}
}
// Defining class Student
public class Student
{
public string Name = "Rahul";
public string Dept = "Electrical";
public int Roll = 10;
public static int id = 02;
}
name is null. Exception Thrown: System.ArgumentNullException
GetMember(String, MemberTypes, BindingFlags) Method
此方法用於使用指定的綁定約束來搜索指定成員類型的指定成員。
用法: public virtual System.Reflection.MemberInfo[] GetMember (string name, System.Reflection.MemberTypes type, System.Reflection.BindingFlags bindingAttr);
參數:
name:它是包含要獲取的成員名稱的字符串。
type:這是要搜索的值。
bindingAttr:這是一個位掩碼,由一個或多個BindingFlags組成,它們指定如何進行搜索,或者指定為零以返回空數組。
返回值:如果發現否則,此方法返回一個MemberInfo對象數組,該數組代表具有指定名稱的公共成員,否則為空數組。
異常:如果名稱為null,則此方法引發ArgumentNullException。
以下示例程序旨在說明上述方法的用法:
示例1:
// C# program to demonstrate the
// Type.GetMember(String, MemberTypes,
// BindingFlags) 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(Student);
// try-catch block for handling Exception
try {
MemberInfo[] info = objType.GetMember("returnNull", MemberTypes.Method,
BindingFlags.Public | BindingFlags.Instance);
// Display the Result
Console.WriteLine("Members 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.WriteLine("name is null.");
Console.Write("Exception Thrown: ");
Console.Write("{0}", e.GetType(), e.Message);
}
}
}
// Defining class Student
public class Student
{
public string Name = "Rahul";
public string Dept = "Electrical";
public int Roll = 10;
public static int id = 02;
public void returnNull() {}
}
Members of current type is as Follow: Void returnNull()
示例2:對於ArgumentNullException
// C# program to demonstrate the
// Type.GetMember(String, MemberTypes,
// BindingFlags) 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(Student);
// try-catch block for handling Exception
try {
MemberInfo[] info = objType.GetMember(null, MemberTypes.Method,
BindingFlags.Public | BindingFlags.Instance);
// Display the Result
Console.WriteLine("Members 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.WriteLine("name is null.");
Console.Write("Exception Thrown: ");
Console.Write("{0}", e.GetType(), e.Message);
}
}
}
// Defining class Student
public class Student
{
public string Name = "Rahul";
public string Dept = "Electrical";
public int Roll = 10;
public static int id = 02;
public void returnNull() {}
}
name is null. Exception Thrown: System.ArgumentNullException
參考:
相關用法
- 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.GetMember() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。