Type.GetMember()方法用于获取当前Type的指定成员。此方法的重载列表中有3种方法,如下所示:
- GetMember(String)方法
- GetMember(String,BindingFlags)方法
- GetMember(String,MemberTypes,BindingFlags)方法
GetMember(String)方法
此方法搜索具有指定名称的公共成员。
Syntax: public System.Reflection.MemberInfo[] GetMember (string name);
Here, it takes the string containing the name of the public members to get.
Return Value: This method returns an array of MemberInfo objects representing the public members with the specified name, if found otherwise, an empty array.
Exception: This method throws ArgumentNullException if name is null.
下面的程序说明了上述方法的用法:
范例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)方法
此方法用于使用指定的绑定约束来搜索指定的成员。
Syntax: public virtual System.Reflection.MemberInfo[] GetMember (string name, System.Reflection.BindingFlags bindingAttr);
Parameters:
name: It is the string containing the name of the members to get.
bindingAttr: It is a bitmask comprised of one or more BindingFlags that specify how the search is conducted. or, Zero, to return an empty array.
Return Value: This method returns an array of MemberInfo objects representing the public members with the specified name if found otherwise, an empty array.
Exception: This method throws ArgumentNullException if name is null.
下面的程序说明了上面讨论的方法的使用:
范例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)方法
此方法用于使用指定的绑定约束来搜索指定成员类型的指定成员。
Syntax: public virtual System.Reflection.MemberInfo[] GetMember (string name, System.Reflection.MemberTypes type, System.Reflection.BindingFlags bindingAttr);
Parameters:
name: It is the string containing the name of the members to get.
type: It is the value to search for.
bindingAttr: It is a bitmask comprised of one or more BindingFlags that specify how the search is conducted or, Zero, to return an empty array.
Return Value: This method returns an array of MemberInfo objects representing the public members with the specified name if found otherwise, an empty array.
Exception: This method throw ArgumentNullException if the name is null.
下面的程序说明了上述方法的用法:
范例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
参考:
- https://docs.microsoft.com/zh-cn/dotnet/api/system.type.getmember?view=netframework-4.8