Skip to content

Instantly share code, notes, and snippets.

@thomyg
Created October 30, 2021 10:10
Show Gist options
  • Save thomyg/b703c7a569177cd1e4f6720c10e4726e to your computer and use it in GitHub Desktop.
Save thomyg/b703c7a569177cd1e4f6720c10e4726e to your computer and use it in GitHub Desktop.
Property Iteration
private static string GetString<T>(T obj)
{
StringBuilder sb = new StringBuilder();
sb.Append("{");
int iteration = 0;
var props = obj.GetType().GetProperties();
foreach (PropertyInfo p in props)
{
iteration++;
sb.Append(p.Name);
sb.Append(":");
string value = p.GetValue(obj, null) != null ? p.GetValue(obj, null).ToString() : "null";
sb.Append(value);
if(iteration<props.Length)
sb.Append(", ");
}
sb.Append("}");
return sb.ToString();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment