Skip to content

Instantly share code, notes, and snippets.

@di-ma-73
Created June 25, 2020 11:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save di-ma-73/25a0a41fb32fb209a8ad5e2cdc20714a to your computer and use it in GitHub Desktop.
Save di-ma-73/25a0a41fb32fb209a8ad5e2cdc20714a to your computer and use it in GitHub Desktop.
/// <summary>
/// Represents object instance properties as a string
/// </summary>
/// <param name="o">Any object to represent its properties</param>
/// <returns>String { prop1: value1 prop2: value2 ... }</returns>
private static string InstancePropertiesToString(object o)
{
var stringBuilder = new StringBuilder("{ ");
foreach (var property in o.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance))
{
stringBuilder.Append($"{property.Name}: {property.GetValue(o) ?? "null"} ");
}
stringBuilder.Append("}");
return stringBuilder.ToString();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment