Formatter.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Reflection;
  6. namespace Bowin.Common.Cache
  7. {
  8. /// <summary>
  9. /// Provides formatting string representing types, methods and fields. The
  10. /// formatting strings may contain arguments like <c>{0}</c>
  11. /// filled at runtime with generic parameters or method arguments.
  12. /// </summary>
  13. internal static class Formatter
  14. {
  15. /// <summary>
  16. /// Gets a formatting string representing a <see cref="Type"/>.
  17. /// </summary>
  18. /// <param name="type">A <see cref="Type"/>.</param>
  19. /// <returns>A formatting string representing the type
  20. /// where each generic type argument is represented as a
  21. /// formatting argument (e.g. <c>Dictionary&lt;{0},P1}&gt;</c>.
  22. /// </returns>
  23. public static string GettypeFormatString(Type type)
  24. {
  25. StringBuilder stringBuilder = new StringBuilder();
  26. // Build the format string for the declaring type.
  27. stringBuilder.Append(type.FullName);
  28. if (type.IsGenericTypeDefinition)
  29. {
  30. stringBuilder.Append("<");
  31. for (int i = 0; i < type.GetGenericArguments().Length; i++)
  32. {
  33. if (i > 0)
  34. stringBuilder.Append(", ");
  35. stringBuilder.AppendFormat("{{{0}}}", i);
  36. }
  37. stringBuilder.Append(">");
  38. }
  39. return stringBuilder.ToString();
  40. }
  41. /// <summary>
  42. /// Gets the formatting strings representing a method.
  43. /// </summary>
  44. /// <param name="method">A <see cref="MethodBase"/>.</param>
  45. /// <returns></returns>
  46. public static MethodFormatStrings GetMethodFormatStrings(MethodBase method)
  47. {
  48. string typeFormat;
  49. bool typeIsGeneric;
  50. string methodFormat;
  51. bool methodIsGeneric;
  52. string parameterFormat;
  53. StringBuilder stringBuilder = new StringBuilder();
  54. typeFormat = GettypeFormatString(method.DeclaringType);
  55. typeIsGeneric = method.DeclaringType.IsGenericTypeDefinition;
  56. // Build the format string for the method name.
  57. stringBuilder.Length = 0;
  58. stringBuilder.Append("::");
  59. stringBuilder.Append(method.Name);
  60. if (method.IsGenericMethodDefinition)
  61. {
  62. methodIsGeneric = true;
  63. stringBuilder.Append("<");
  64. for (int i = 0; i < method.GetGenericArguments().Length; i++)
  65. {
  66. if (i > 0)
  67. stringBuilder.Append(", ");
  68. stringBuilder.AppendFormat("{{{0}}}", i);
  69. }
  70. stringBuilder.Append(">");
  71. }
  72. else
  73. {
  74. methodIsGeneric = false;
  75. }
  76. methodFormat = stringBuilder.ToString();
  77. // Build the format string for parameters.
  78. stringBuilder.Length = 0;
  79. ParameterInfo[] parameters = method.GetParameters();
  80. stringBuilder.Append("(");
  81. for (int i = 0; i < parameters.Length; i++)
  82. {
  83. if (i > 0)
  84. {
  85. stringBuilder.Append(", ");
  86. }
  87. stringBuilder.Append("{{{");
  88. stringBuilder.Append(i);
  89. stringBuilder.Append("}}}");
  90. }
  91. stringBuilder.Append(")");
  92. parameterFormat = stringBuilder.ToString();
  93. return new MethodFormatStrings(typeFormat, typeIsGeneric, methodFormat, methodIsGeneric, parameterFormat);
  94. }
  95. /// <summary>
  96. /// Pads a string with a space, if not empty and not yet padded.
  97. /// </summary>
  98. /// <param name="prefix">A string.</param>
  99. /// <returns>A padded string.</returns>
  100. public static string NormalizePrefix(string prefix)
  101. {
  102. if (string.IsNullOrEmpty(prefix))
  103. {
  104. return "";
  105. }
  106. else if (prefix.EndsWith(" "))
  107. {
  108. return prefix;
  109. }
  110. else
  111. {
  112. return prefix + " ";
  113. }
  114. }
  115. public static string FormatString(string format, params object[] args)
  116. {
  117. if (args == null)
  118. return format;
  119. else
  120. return string.Format(format, args);
  121. }
  122. }
  123. }