Extensions.cs 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. namespace SuperWebSocket
  6. {
  7. /// <summary>
  8. /// Extension class
  9. /// </summary>
  10. public static partial class Extensions
  11. {
  12. private readonly static char[] m_CrCf;
  13. static Extensions()
  14. {
  15. m_CrCf = "\r\n".ToArray();
  16. }
  17. /// <summary>
  18. /// Appends in the format with CrCf as suffix.
  19. /// </summary>
  20. /// <param name="builder">The builder.</param>
  21. /// <param name="format">The format.</param>
  22. /// <param name="arg">The arg.</param>
  23. public static void AppendFormatWithCrCf(this StringBuilder builder, string format, object arg)
  24. {
  25. builder.AppendFormat(format, arg);
  26. builder.Append(m_CrCf);
  27. }
  28. /// <summary>
  29. /// Appends in the format with CrCf as suffix.
  30. /// </summary>
  31. /// <param name="builder">The builder.</param>
  32. /// <param name="format">The format.</param>
  33. /// <param name="args">The args.</param>
  34. public static void AppendFormatWithCrCf(this StringBuilder builder, string format, params object[] args)
  35. {
  36. builder.AppendFormat(format, args);
  37. builder.Append(m_CrCf);
  38. }
  39. /// <summary>
  40. /// Appends with CrCf as suffix.
  41. /// </summary>
  42. /// <param name="builder">The builder.</param>
  43. /// <param name="content">The content.</param>
  44. public static void AppendWithCrCf(this StringBuilder builder, string content)
  45. {
  46. builder.Append(content);
  47. builder.Append(m_CrCf);
  48. }
  49. /// <summary>
  50. /// Appends with CrCf as suffix.
  51. /// </summary>
  52. /// <param name="builder">The builder.</param>
  53. public static void AppendWithCrCf(this StringBuilder builder)
  54. {
  55. builder.Append(m_CrCf);
  56. }
  57. private static Type[] m_SimpleTypes = new Type[] {
  58. typeof(String),
  59. typeof(Decimal),
  60. typeof(DateTime),
  61. typeof(DateTimeOffset),
  62. typeof(TimeSpan),
  63. typeof(Guid)
  64. };
  65. internal static bool IsSimpleType(this Type type)
  66. {
  67. return
  68. type.IsValueType ||
  69. type.IsPrimitive ||
  70. m_SimpleTypes.Contains(type) ||
  71. Convert.GetTypeCode(type) != TypeCode.Object;
  72. }
  73. }
  74. }