Created
December 1, 2020 02:26
-
-
Save KuroiLight/f856771e60024cd3262f3dd1fd6d5dca to your computer and use it in GitHub Desktop.
reflection based tostring
This file contains 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
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