- using System;
- using System.Reflection;
- namespace forgetCode
- {
- static class ReflectionTest
- {
- public static int Length;
- public static int Width;
- public static int Weight;
- public static string Name;
- public static void Write()
- {
- Type type = typeof(ReflectionTest); // Get type pointer
- FieldInfo[] fields = type.GetFields(); // Obtain all fields
- foreach (var field in fields) // Loop through fields
- {
- string name = field.Name; // Get string name
- object temp = field.GetValue(null); // Get value
- if (temp is int) // See if it is an integer.
- {
- int value = (int)temp;
- Console.Write(name);
- Console.Write(" (int) = ");
- Console.WriteLine(value);
- }
- else if (temp is string) // See if it is a string.
- {
- string value = temp as string;
- Console.Write(name);
- Console.Write(" (string) = ");
- Console.WriteLine(value);
- }
- }
- }
- }
- class Program
- {
- static void Main()
- {
- ReflectionTest.Length = 100; // Set value
- ReflectionTest.Width = 50; // Set value
- ReflectionTest.Weight = 300; // Set value
- ReflectionTest.Name = "Forget Code"; // Set value
- ReflectionTest.Write(); // Invoke reflection methods
- }
- }
- }