Type.GetField()方法用于获取当前Type的特定字段。此方法的重载列表中有2种方法,如下所示:
- GetField(String)方法
- GetField(String,BindingFlags)方法
GetField(String)方法
此方法用于搜索具有指定名称的公共字段。
Syntax: public System.Reflection.FieldInfo GetField (string name);
Here, it takes the string containing the name of the data field to get.
Return Value: This method returns an object representing the public field with the specified name if found otherwise, null.
Exception: This method throws ArgumentNullException if the name is null .
下面的程序说明了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)方法
此方法用于使用指定的绑定约束来搜索指定的字段。
Syntax: public abstract System.Reflection.FieldInfo GetField (string name, System.Reflection.BindingFlags bindingAttr);
Parameters:
name: The string containing the name of the data field to get.
bindingAttr: It is a bitmask comprised of one or more BindingFlags that specify how the search is conducted or Zero, to return 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
参考:
- https://docs.microsoft.com/zh-cn/dotnet/api/system.type.getfield?view=netframework-4.8