Here is a slightly modified example from
C# | Type.GetFields() Method - GeeksforGeeks[
^] that lists the public variables from a class:
using System;
using System.Reflection;
public class GFG {
public static void Main()
{
Type objType = typeof(Student);
try {
FieldInfo[] info = objType.GetFields(BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static);
Console.WriteLine("Fields of current type is as Follows: ");
for (int i = 0; i < info.Length; i++)
Console.WriteLine(" {0}", info[i]);
}
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;
public static int id = 02;
}