Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Get a specific field of the current type C#
To get a specific field of the current type in C#, you use the GetField() method from the System.Reflection namespace. This method returns a FieldInfo object that represents the field with the specified name, or null if the field is not found.
Syntax
Following is the syntax for getting a specific field using reflection −
Type type = typeof(ClassName);
FieldInfo fieldInfo = type.GetField("fieldName");
The GetField() method has several overloads that accept binding flags to control the search behavior −
FieldInfo fieldInfo = type.GetField("fieldName", BindingFlags.Public | BindingFlags.Instance);
Parameters
name − The string containing the name of the data field to get.
bindingAttr (optional) − A bitmask comprised of one or more BindingFlags that specify how the search is conducted.
Return Value
Returns a FieldInfo object representing the field that matches the specified requirements, or null if the field is not found.
Getting a Public Field
This example demonstrates retrieving a public field from a class −
using System;
using System.Reflection;
public class Demo {
public static void Main() {
Type type = typeof(Subject);
try {
FieldInfo fieldInfo = type.GetField("SubName");
Console.WriteLine("FieldInfo = {0}", fieldInfo);
Console.WriteLine("Field Type = {0}", fieldInfo.FieldType);
Console.WriteLine("Field Name = {0}", fieldInfo.Name);
}
catch (ArgumentNullException e) {
Console.Write("{0}", e.GetType(), e.Message);
}
}
}
public class Subject {
public string SubName = "Science";
}
The output of the above code is −
FieldInfo = System.String SubName Field Type = System.String Field Name = SubName
Getting Field Value
You can also retrieve the actual value of a field from an object instance −
using System;
using System.Reflection;
public class Demo {
public static void Main() {
Subject subject = new Subject();
Type type = typeof(Subject);
FieldInfo fieldInfo = type.GetField("SubName");
if (fieldInfo != null) {
object fieldValue = fieldInfo.GetValue(subject);
Console.WriteLine("Field Value = {0}", fieldValue);
// Set a new value
fieldInfo.SetValue(subject, "Mathematics");
Console.WriteLine("New Field Value = {0}", subject.SubName);
}
}
}
public class Subject {
public string SubName = "Science";
}
The output of the above code is −
Field Value = Science New Field Value = Mathematics
Handling ArgumentNullException
When you pass null as the field name, an ArgumentNullException is thrown −
using System;
using System.Reflection;
public class Demo {
public static void Main() {
Type type = typeof(Subject);
try {
FieldInfo fieldInfo = type.GetField(null);
Console.WriteLine("FieldInfo = {0}", fieldInfo);
}
catch (ArgumentNullException e) {
Console.WriteLine("Exception: {0}", e.GetType().Name);
Console.WriteLine("Message: {0}", e.Message);
}
}
}
public class Subject {
public string SubName = "Science";
}
The output of the above code is −
Exception: ArgumentNullException Message: Value cannot be null. Parameter name: name
Common Use Cases
Dynamic field access − When field names are determined at runtime.
Serialization/Deserialization − Reading and writing object data to different formats.
Property mapping − Copying values between objects with similar structures.
Debugging and testing − Inspecting object state during development.
Conclusion
The GetField() method in C# reflection provides a powerful way to access field information at runtime. It returns a FieldInfo object that can be used to read field metadata, get values, and set values dynamically, making it essential for scenarios requiring runtime type inspection.
