📜  C#| Type.GetMember()方法

📅  最后修改于: 2021-05-29 13:25:02             🧑  作者: Mango

Type.GetMember()方法用于获取当前Type的指定成员。此方法的重载列表中有3种方法,如下所示:

  • GetMember(String)方法
  • GetMember(String,BindingFlags)方法
  • GetMember(String,MemberTypes,BindingFlags)方法

GetMember(String)方法

此方法搜索具有指定名称的公共成员。

下面的程序说明了上述方法的用法:

范例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)方法

此方法用于使用指定的绑定约束来搜索指定的成员。

下面的程序说明了上面讨论的方法的使用:

范例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)方法

此方法用于使用指定的绑定约束来搜索指定成员类型的指定成员。

下面的程序说明了上述方法的用法:

范例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