Created
March 25, 2021 14:32
-
-
Save roboryantron/af604cadf58156235e8cf7a948a331d4 to your computer and use it in GitHub Desktop.
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
| public override string ToString() | |
| { | |
| const BindingFlags flags = BindingFlags.Public | BindingFlags.NonPublic | | |
| BindingFlags.IgnoreCase | BindingFlags.Instance; | |
| string result = $"{GetType().Name}:"; | |
| FieldInfo[] fields = GetType().GetFields(flags); | |
| for (int i = 0; i < fields.Length; i++) | |
| { | |
| result += $"\n {fields[i].Name} = {fields[i].GetValue(this)}"; | |
| } | |
| return result; | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Depending on how much this is getting used, you could use a static string builder rather than the constant concatenation. You could also cache the field info list to reduce reflection