StringUtil.cs 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. using System.Collections.Generic;
  2. using System.Text;
  3. namespace Luban
  4. {
  5. public static class StringUtil
  6. {
  7. public static string ToStr(object o)
  8. {
  9. return ToStr(o, new StringBuilder());
  10. }
  11. public static string ToStr(object o, StringBuilder sb)
  12. {
  13. foreach (var p in o.GetType().GetFields())
  14. {
  15. sb.Append($"{p.Name} = {p.GetValue(o)},");
  16. }
  17. foreach (var p in o.GetType().GetProperties())
  18. {
  19. sb.Append($"{p.Name} = {p.GetValue(o)},");
  20. }
  21. return sb.ToString();
  22. }
  23. public static string ArrayToString<T>(T[] arr)
  24. {
  25. return "[" + string.Join(",", arr) + "]";
  26. }
  27. public static string CollectionToString<T>(IEnumerable<T> arr)
  28. {
  29. return "[" + string.Join(",", arr) + "]";
  30. }
  31. public static string CollectionToString<TK, TV>(IDictionary<TK, TV> dic)
  32. {
  33. var sb = new StringBuilder();
  34. sb.Append('{');
  35. foreach (var e in dic)
  36. {
  37. sb.Append(e.Key).Append(':');
  38. sb.Append(e.Value).Append(',');
  39. }
  40. sb.Append('}');
  41. return sb.ToString();
  42. }
  43. }
  44. }