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 the fields of the current Type in C#
To get the fields of the current Type in C#, you can use the GetFields() method from the System.Reflection namespace. This method allows you to retrieve field information using various BindingFlags to specify which types of fields to include.
Reflection in C# provides the ability to inspect and manipulate types, methods, properties, and fields at runtime. The Type.GetFields() method is particularly useful for examining the internal structure of classes.
Syntax
Following is the basic syntax for getting fields using reflection −
Type type = typeof(ClassName); FieldInfo[] fields = type.GetFields();
To get specific types of fields, use BindingFlags −
FieldInfo[] fields = type.GetFields(BindingFlags.Static | BindingFlags.NonPublic);
Using GetFields() with String Type
The following example demonstrates how to retrieve non-public static fields from the String type −
using System;
using System.Reflection;
public class Demo {
public static void Main() {
Type type = typeof(System.String);
FieldInfo[] fields = type.GetFields(BindingFlags.Static | BindingFlags.NonPublic);
Console.WriteLine("Following are the non-public fields=");
foreach (FieldInfo myField in fields) {
Console.WriteLine(myField.ToString());
}
}
}
The output of the above code is −
Following are the non-public fields= Int32 TrimHead Int32 TrimTail Int32 TrimBoth Int32 charPtrAlignConst Int32 alignConst
Using GetFields() with Char Type
Here's another example that retrieves non-public static fields from the Char type −
using System;
using System.Reflection;
public class Demo {
public static void Main() {
Type type = typeof(System.Char);
FieldInfo[] fields = type.GetFields(BindingFlags.Static | BindingFlags.NonPublic);
Console.WriteLine("Following are the non-public fields=");
foreach (FieldInfo myField in fields) {
Console.WriteLine(myField.ToString());
}
}
}
The output of the above code is −
Following are the non-public fields= Byte[] categoryForLatin1 Int32 UNICODE_PLANE00_END Int32 UNICODE_PLANE01_START Int32 UNICODE_PLANE16_END Int32 HIGH_SURROGATE_START Int32 LOW_SURROGATE_END
Using GetFields() with Custom Class
The following example shows how to get fields from a custom class −
using System;
using System.Reflection;
public class Student {
public string name;
private int age;
public static string school = "ABC School";
}
public class Demo {
public static void Main() {
Type type = typeof(Student);
Console.WriteLine("All Public Fields:");
FieldInfo[] publicFields = type.GetFields(BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static);
foreach (FieldInfo field in publicFields) {
Console.WriteLine(field.Name + " - " + field.FieldType);
}
Console.WriteLine("\nAll Fields (including private):");
FieldInfo[] allFields = type.GetFields(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static);
foreach (FieldInfo field in allFields) {
Console.WriteLine(field.Name + " - " + field.FieldType);
}
}
}
The output of the above code is −
All Public Fields: name - System.String school - System.String All Fields (including private): name - System.String age - System.Int32 school - System.String
Common BindingFlags
| BindingFlag | Description |
|---|---|
| Public | Includes public fields |
| NonPublic | Includes private, protected, and internal fields |
| Static | Includes static fields |
| Instance | Includes instance (non-static) fields |
Conclusion
The GetFields() method with appropriate BindingFlags allows you to inspect any type's field structure at runtime. This is particularly useful for debugging, serialization, or building generic utilities that work with unknown types dynamically.
