當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


C# Type.GetField()用法及代碼示例


Type.GetField()方法用於獲取當前Type的特定字段。此方法的重載列表中有2種方法,如下所示:

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

GetField(String) Method

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


用法: public System.Reflection.FieldInfo GetField (string name);
Here, it takes the string containing the name of the data field to get.

返回值:如果找到,則此方法返回一個具有指定名稱的代表公共字段的對象,否則為null。

異常:如果名稱為null,則此方法拋出ArgumentNullException。

以下示例程序旨在說明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) Method

此方法用於使用指定的綁定約束來搜索指定的字段。

用法: public abstract System.Reflection.FieldInfo GetField (string name, System.Reflection.BindingFlags bindingAttr);

參數

name:包含要獲取的數據字段名稱的字符串。
bindingAttr:這是一個位掩碼,由一個或多個BindingFlags組成,它們指定進行搜索的方式或為零,以返回null。

返回值:如果發現否則,此方法返回一個對象,該對象表示與指定要求匹配的字段,否則為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

參考:



相關用法


注:本文由純淨天空篩選整理自RohitPrasad3大神的英文原創作品 C# | Type.GetField() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。