C# | Type.GetField() Method
Type.GetField() Method is used to get a specific field of the current Type. There are 2 methods in the overload list of this method as follows:
- GetField(String) Method
- GetField(String, BindingFlags) Method
GetField(String) Method
This method is used to search for the public field with the specified name.
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 .
Below programs illustrate the use of Type.GetField() Method:
Example 1:
using System;
using System.Globalization;
using System.Reflection;
class GFG {
public static void Main()
{
Type objType = typeof (Student);
try {
FieldInfo info = objType.GetField( "Name" );
Console.WriteLine( "FieldInfo is: {0}" , info);
}
catch (ArgumentNullException e)
{
Console.Write( "name is null." );
Console.Write( "Exception Thrown: " );
Console.Write( "{0}" , e.GetType(), e.Message);
}
}
}
public class Student
{
public string Name = "Rahul" ;
public string Dept = "Electrical" ;
public int Roll = 10;
}
|
Output:
FieldInfo is: System.String Name
Example 2: For ArgumentNullException
using System;
using System.Globalization;
using System.Reflection;
class GFG {
public static void Main()
{
Type objType = typeof (Student);
try {
FieldInfo info = objType.GetField( null );
Console.WriteLine( "FieldInfo is: {0}" , info);
}
catch (ArgumentNullException e)
{
Console.WriteLine( "Name is null." );
Console.Write( "Exception Thrown: " );
Console.Write( "{0}" , e.GetType(), e.Message);
}
}
}
public class Student
{
public string Name = "Rahul" ;
public string Dept = "Electrical" ;
public int Roll = 10;
}
|
Output:
name is null.
Exception Thrown: System.ArgumentNullException
GetField(String, BindingFlags) Method
This method is used to search for the specified field, using the specified binding constraints.
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.
Return Value: This method returns an object representing the field that matches the specified requirements if found otherwise, null.
Exception: This method throws ArgumentNullException if name is null.
Below programs illustrate the use of the above-discussed method:
Example 1:
using System;
using System.Globalization;
using System.Reflection;
class GFG {
public static void Main()
{
Type objType = typeof (Student);
try {
BindingFlags battr = BindingFlags.Public | BindingFlags.Instance;
FieldInfo info = objType.GetField( "Name" , battr);
Console.WriteLine( "FieldInfo is: {0}" , info);
}
catch (ArgumentNullException e)
{
Console.Write( "name is null." );
Console.Write( "Exception Thrown: " );
Console.Write( "{0}" , e.GetType(), e.Message);
}
}
}
public class Student
{
public string Name = "Rahul" ;
public string Dept = "Electrical" ;
public int Roll = 10;
}
|
Output:
FieldInfo is: System.String Name
Example 2: For ArgumentNullException
using System;
using System.Globalization;
using System.Reflection;
class GFG {
public static void Main()
{
Type objType = typeof (Student);
try {
BindingFlags battr = BindingFlags.Public | BindingFlags.Instance;
FieldInfo info = objType.GetField( null , battr);
Console.WriteLine( "FieldInfo is: {0}" , info);
}
catch (ArgumentNullException e)
{
Console.WriteLine( "name is null." );
Console.Write( "Exception Thrown: " );
Console.Write( "{0}" , e.GetType(), e.Message);
}
}
}
public class Student
{
public string Name = "Rahul" ;
public string Dept = "Electrical" ;
public int Roll = 10;
}
|
Output:
name is null.
Exception Thrown: System.ArgumentNullException
Reference:
Last Updated :
10 Dec, 2019
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...