// Collection of code snippets by Arne Vajhøj // (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) using System; using System.Collections; using System.Collections.Generic; using System.Reflection; namespace E { public class Foo { public override string ToString() { return "I am a Foo"; } } public class Bar { public override string ToString() { return "I am a Bar"; } } public class MyClass { public string Name { get; set; } public List Foos { get; set; } public List Bars { get; set; } public MyClass() { Foos = new List(); Bars = new List(); } } public class Program { public static void Dump(object o) { Console.Write(o.GetType().FullName + ":"); foreach(PropertyInfo pi in o.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public)) { Console.Write(" "); if(typeof(IList).IsAssignableFrom(pi.PropertyType)) { Console.Write(pi.Name + "=["); IList elms = (IList)pi.GetValue(o, null); for(int i = 0; i < elms.Count; i++) { if(i != 0) { Console.Write(","); } Console.Write(elms[i]); } Console.Write("]"); } else { Console.Write(pi.Name + "=" + pi.GetValue(o, null)); } } Console.WriteLine(); } public static void Main(string[] args) { MyClass o = new MyClass(); o.Name = "Test"; o.Foos.Add(new Foo()); o.Bars.Add(new Bar()); o.Bars.Add(new Bar()); Dump(o); } } }