当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


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