Created
October 30, 2021 10:10
-
-
Save thomyg/b703c7a569177cd1e4f6720c10e4726e to your computer and use it in GitHub Desktop.
Property Iteration
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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