using System.Collections.Generic;
public static void Main()
Console.WriteLine("Dummy1:");
var dummy1 = new Dummy();
var propertyValues1 = GetPropertyValues<Dummy>(dummy1, false);
PrintPropertyValues(propertyValues1);
Console.WriteLine("Dummy2:");
var dummy2 = new Dummy { SomeString = "text", SomeInt = 5 };
var propertyValues2 = GetPropertyValues<Dummy>(dummy2, false);
PrintPropertyValues(propertyValues2);
Console.WriteLine("Dummy3:");
var dummy3 = new Dummy { SomeInt = 5, SomeNullableInt = 10 };
var propertyValues3 = GetPropertyValues<Dummy>(dummy3, false);
PrintPropertyValues(propertyValues3);
public static void PrintPropertyValues(Dictionary<string, string> propertyValues)
foreach (var property in propertyValues)
Console.WriteLine("KEY: " + property.Key + ", VALUE: " + property.Value);
public static Dictionary<string, string> GetPropertyValues<T>(T obj, bool allowNull = false)
var propertyValues = new Dictionary<string, string>();
PropertyInfo[] properties = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance);
foreach (PropertyInfo property in properties)
object propertyValue = property.GetValue(obj);
if (!allowNull && propertyValue == null)
propertyValues.Add(property.Name, propertyValue.ToString());
public string SomeString { get; set; }
public int SomeInt { get; set; }
public int? SomeNullableInt { get; set; }