Skip to content

Instantly share code, notes, and snippets.

@KuroiLight
Created December 1, 2020 02:26
Show Gist options
  • Save KuroiLight/f856771e60024cd3262f3dd1fd6d5dca to your computer and use it in GitHub Desktop.
Save KuroiLight/f856771e60024cd3262f3dd1fd6d5dca to your computer and use it in GitHub Desktop.
reflection based tostring
public override string ToString()
{
StringBuilder sb = new();
var fields = this.GetType().GetProperties(System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public);
foreach (var item in fields)
{
if (item.CanRead)
{
sb.AppendFormat(CultureInfo.CurrentCulture, "{0}: {1}, ", item.Name, item.GetValue(this));
}
}
return sb.ToString().TrimEnd(new char[] { ' ', ',' });
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment