using System.Collections.Generic;
public static void Main()
var obj1 = new { Name = "Bubba" };
dynamic obj2 = new TestClass { Name = "Steve" };
var obj3 = new TestClass { Name = "Paul" };
dynamic obj4 = new ExpandoObject();
var results1 = GetProperties( obj1 );
var results2 = GetProperties( obj2 );
var results3 = GetProperties( obj3 );
var results4 = GetProperties( obj4 );
Console.WriteLine($"Obj1 = {ToKVString(results1)}");
Console.WriteLine($"Obj2 = {ToKVString(results2)}");
Console.WriteLine($"Obj3 = {ToKVString(results3)}");
Console.WriteLine($"Obj4 = {ToKVString(results4)}");
public static Dictionary<string, string> GetProperties( dynamic obj )
Dictionary<string, string> results = new Dictionary<string, string>();
PropertyInfo[] props = obj.GetType().GetProperties();
if ( props != null && props.Length > 0 )
Array.ForEach<PropertyInfo> (props, p => results.Add( p.Name, (p.CanRead) ? p.GetValue(obj).ToString() : String.Empty ) );
public static string ToKVString( Dictionary<string, string> obj )
StringBuilder sb = new StringBuilder();
obj.Keys.ToList().ForEach( k => sb.Append($"{k}={obj[k]};") );
public string Name { get; set; }