📜  C#| Type.GetField()方法

📅  最后修改于: 2021-05-29 16:45:45             🧑  作者: Mango

Type.GetField()方法用于获取当前Type的特定字段。此方法的重载列表中有2种方法,如下所示:

  • GetField(String)方法
  • GetField(String,BindingFlags)方法

GetField(String)方法

此方法用于搜索具有指定名称的公共字段。

下面的程序说明了Type.GetField()方法的用法:

范例1:

// C# program to demonstrate the
// Type.GetField(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 {
  
            // Getting FieldInfo by
            // using GetField(String) Method
            FieldInfo info = objType.GetField("Name");
  
            // Display the Result
            Console.WriteLine("FieldInfo is: {0}", info);
        }
  
        // 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;
}
输出:
FieldInfo is: System.String Name

示例2:对于ArgumentNullException

// C# program to demonstrate the
// Type.GetField(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 {
  
            // Getting FieldInfo by
            // using GetField() Method
            FieldInfo info = objType.GetField(null);
  
            // Display the Result
            Console.WriteLine("FieldInfo is: {0}", info);
        }
  
        // 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;
}
输出:
name is null.
Exception Thrown: System.ArgumentNullException

GetField(String,BindingFlags)方法

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

返回值:如果发现否则,此方法返回一个表示与指定要求匹配的字段的对象,否则为null。

异常:如果name为null,则此方法将引发ArgumentNullException。

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

范例1:

// C# program to demonstrate the
// Type.GetField(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 {
  
            // You must specify either BindingFlags.Instance
            // or BindingFlags.Static
            // and Specify BindingFlags.Public
            // to include public fields in the search
            BindingFlags battr = BindingFlags.Public | BindingFlags.Instance;
  
            // Getting FieldInfo by
            // using GetField() Method
            FieldInfo info = objType.GetField("Name", battr);
  
            // Display the Result
            Console.WriteLine("FieldInfo is: {0}", info);
        }
  
        // 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;
}
输出:
FieldInfo is: System.String Name

示例2:对于ArgumentNullException

// C# program to demonstrate the
// Type.GetField(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 {
  
            // You must specify either BindingFlags.Instance 
            // or BindingFlags.Static
            // and Specify BindingFlags.Public
            // to include public fields in the search
            BindingFlags battr = BindingFlags.Public | BindingFlags.Instance;
  
            // Getting FieldInfo by
            // using GetField() Method
            FieldInfo info = objType.GetField(null, battr);
  
            // Display the Result
            Console.WriteLine("FieldInfo is: {0}", info);
        }
  
        // 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;
}

输出:

name is null.
Exception Thrown: System.ArgumentNullException

参考:

  • https://docs.microsoft.com/zh-cn/dotnet/api/system.type.getfield?view=netframework-4.8