fork(1) download
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. public class MyClass
  6. {
  7. public string StrProp1 { get; set; }
  8. public string StrProp2 { get; set; }
  9. public int IntProp { get; set; }
  10. public DateTime? DTNullableProp { get; set; }
  11. public int? IntNullableProp { get; set; }
  12. }
  13.  
  14. public class Test
  15. {
  16. public static void Main()
  17. {
  18. var o = new MyClass()
  19. {
  20. StrProp1 = "p1", StrProp2 = "p2",
  21. IntProp = 2, IntNullableProp = null,
  22. DTNullableProp = DateTime.Now
  23. };
  24. foreach (var kv in GetDictionary(o))
  25. Console.WriteLine("o.{0} = {1}", kv.Key, kv.Value);
  26. }
  27.  
  28. static Dictionary<string, string> GetDictionary(object o)
  29. {
  30. return o.GetType().GetProperties().ToDictionary(p => p.Name, p => p.GetValue(o)?.ToString());
  31. }
  32. }
Success #stdin #stdout 0.09s 24272KB
stdin
Standard input is empty
stdout
o.StrProp1 = p1
o.StrProp2 = p2
o.IntProp = 2
o.DTNullableProp = 11/25/2015 2:39:12 PM
o.IntNullableProp =