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


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


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

    • GetFields()方法
    • GetFields(BindingFlags)方法

GetFields() Method

此方法用於返回當前Type的所有公共字段。


用法: public System.Reflection.FieldInfo[] GetFields ();

返回值:此方法返回一個FieldInfo對象數組,該對象代表為當前Type定義的所有公共字段。或者,如果沒有為當前Type定義公共字段,則為FieldInfo類型的空數組。

以下示例程序旨在說明Type.GetFields()方法的使用:

示例1:

// C# program to demonstrate the 
// Type.GetFields() 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 array of Fields by 
            // using GetField() Method 
            FieldInfo[] info = objType.GetFields(BindingFlags.Public | BindingFlags.Static); 
  
            // Display the Result 
            Console.Write("Fields 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; 
}
輸出:
Fields of current type is as Follow:  System.Int32 id

示例2:如果沒有定義公共領域

// C# program to demonstrate the 
// Type.GetFields(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 array of Fields by 
            // using GetField() Method 
            FieldInfo[] info = objType.GetFields(); 
  
            // Display the Result 
            Console.Write("Public Fields of current type is as follow: "); 
            if (info.Length != 0)  
            { 
                for (int i = 0; i < info.Length; i++) 
                    Console.WriteLine(" {0}", info[i]); 
            } 
            else
                Console.WriteLine("No public fields are defined for the current Type."); 
        } 
  
        // 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 Fields of current type is as follow: No public fields are defined for the current Type.

GetFields(BindingFlags) Method

此方法用於在為派生類覆蓋約束時使用指定的綁定來搜索為當前Type定義的字段。

用法: public abstract System.Reflection.FieldInfo[] GetFields (System.Reflection.BindingFlags bindingAttr);
Here, bindingAttr is a bitmask comprised of one or more BindingFlags that specify how the search is conducted or Zero, to return null.

返回值:此方法返回一個FieldInfo對象的數組,該對象表示為當前Type定義的所有與指定綁定約束匹配的字段。或者,如果沒有為當前Type定義任何字段,或者沒有定義的字段與綁定約束匹配,則為FieldInfo類型的空數組。


以下示例程序旨在說明上述方法的用法:

示例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); 
     
        // Creating 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.GetFields(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(Book); 
     
        // Creating try-catch block for handling Exception  
        try {  
     
            // Getting array of Fields by   
            // using GetField() Method  
            FieldInfo[] info = objType.GetFields(BindingFlags.NonPublic | BindingFlags.Instance);  
     
            // Display the Result 
            Console.WriteLine("Fields 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 Book 
{ 
    public string Name = "Element of Physics"; 
    public string Author = "R.S. Agrwal"; 
    public static int Price = 500; 
    private string metadata = "djdieeiie"; 
   
}
輸出:
FieldInfo is - System.String Name

參考:



相關用法


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