Månad: mars 2009

A very simple object dumper

static void Write<T>(T item) where T : class, new()
        {
            if (item == null) { item = new T(); }
            var t = item.GetType();
            Console.WriteLine(string.Format("Class name:{0}{2}Namespace:{1}{2}", t.Name, t.Namespace, Environment.NewLine));
            foreach (var propInfo in t.GetProperties())
            {
                if (propInfo.PropertyType.IsPublic && propInfo.PropertyType.IsPrimitive)
                {
                    var propValue = t.GetProperty(propInfo.Name).GetValue(item, null);
                    Console.WriteLine(string.Format("Property name:{0} Value:{1}", propInfo.Name, propValue));
                }
            }
            Console.WriteLine(Environment.NewLine);
        }