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


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