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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。