// Ex 4: Use reflection to invoke a method dynamically public void Ex4_Method() { ExClassHierarchy ech = new ExClassHierarchy(); MethodInfo m1 = ech.GetType().GetMethod("Ex2_Base", new Type[] {}); m1.Invoke(ech, new object[] { }); }
// Ex 3: Use reflection to inspect property/field public void Ex3_Property() { ExClassHierarchy ech = new ExClassHierarchy(); // Get private, non-static fields FieldInfo[] fields = ech.GetType().GetFields(BindingFlags.Instance | BindingFlags.NonPublic); foreach (var f in fields) { Console.WriteLine("Field: {0}", f.ToString()); } // Get public, instance properties PropertyInfo[] props = ech.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public); foreach (var p in props) { Console.WriteLine("Property: {0}", p.ToString()); } }