forked from Byte-Nova/Rimworld-Together
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Can now Serialize any object to debugging string
- Loading branch information
Showing
2 changed files
with
115 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
using System.Reflection; | ||
using System; | ||
using System.Collections; | ||
|
||
namespace Shared | ||
{ | ||
public static class StringUtilities | ||
{ | ||
public static string ToString(object obj, int tabbing = 0) | ||
{ | ||
string str = ""; | ||
if (tabbing == 0) | ||
{ | ||
str += Indent(tabbing) + $"{obj.GetType().Name}:\n"; | ||
tabbing++; | ||
} | ||
foreach (FieldInfo field in obj.GetType().GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance)) | ||
{ | ||
object value = field.GetValue(obj); | ||
str += Indent(tabbing); | ||
|
||
if (value != null) | ||
{ | ||
if (field.FieldType.IsPrimitive || field.FieldType.IsValueType || field.FieldType == typeof(string)) | ||
{ | ||
str += $"{field.Name}: {value}\n"; // Is Primitive | ||
} | ||
else if (value is Array) // Is Array | ||
{ | ||
str += $"{field.Name}: {HandleArray((Array)value, tabbing + 1)}"; | ||
} | ||
else if (typeof(IEnumerable).IsAssignableFrom(field.FieldType) && field.FieldType != typeof(string)) // Is List | ||
{ | ||
str += $"{field.Name}: {HandleIEnumerable((IEnumerable)value, tabbing + 1)}"; | ||
} | ||
else //Is Object | ||
{ | ||
str += $"{field.Name}: \n{ToString(value, tabbing + 1)}"; | ||
} | ||
} | ||
else | ||
{ | ||
str += $"{field.Name}: was null\n"; // Is Null | ||
} | ||
} | ||
return str; | ||
} | ||
|
||
private static string HandleArray(Array array, int tabbing) | ||
{ | ||
string str = ""; | ||
if(array.Length == 0) | ||
{ | ||
return str += Indent(tabbing) + "Empty"; | ||
} | ||
str += "\n"; | ||
foreach (var item in array) | ||
{ | ||
str += "" + ProcessItem(item, tabbing); | ||
} | ||
|
||
return str; | ||
} | ||
|
||
private static string HandleIEnumerable(IEnumerable enumerable, int tabbing) | ||
{ | ||
string str = ""; | ||
if (!enumerable.GetEnumerator().MoveNext()) | ||
{ | ||
return str += "Empty\n"; | ||
} | ||
foreach (var item in enumerable) | ||
{ | ||
str += ProcessItem(item, tabbing); | ||
} | ||
|
||
return str; | ||
} | ||
|
||
private static string ProcessItem(object item, int tabbing) | ||
{ | ||
string str = ""; | ||
str += Indent(tabbing); | ||
if (item != null) | ||
{ | ||
if (item.GetType().IsPrimitive || item.GetType().IsValueType || item.GetType() == typeof(string)) // Is primitive | ||
{ | ||
str += $"{item.GetType().Name}: {item}\n"; | ||
} | ||
else if (item.GetType() == typeof(IEnumerable)) //Is List | ||
{ | ||
str += $"{item.GetType().Name}: \n{HandleIEnumerable((IEnumerable)item, tabbing + 1)}"; | ||
} | ||
else if (item.GetType() == typeof(Array)) //Is Array | ||
{ | ||
str += $"{item.GetType().Name}: \n{HandleArray((Array)item, tabbing + 1)}"; | ||
} | ||
else //Is Object | ||
{ | ||
str += $"{item.GetType().Name}: \n{ToString(item, tabbing + 1)}"; | ||
} | ||
} | ||
else | ||
{ | ||
str += $"{item.GetType().Name}: was null\n"; // Is Null | ||
} | ||
return str; | ||
} | ||
private static string Indent(int level) | ||
{ | ||
return new string('\t', level); | ||
} | ||
} | ||
} |
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