MethodFormatStrings.cs 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. /// Set of 3 formatting string that, at runtime, represent a method and its
  10. /// parameters.
  11. /// </summary>
  12. [Serializable]
  13. internal class MethodFormatStrings
  14. {
  15. private readonly string typeFormat;
  16. private readonly string methodFormat;
  17. private readonly string parameterFormat;
  18. private readonly bool typeIsGeneric;
  19. private readonly bool methodIsGeneric;
  20. /// <summary>
  21. /// Initializes a new <see cref="MethodFormatStrings"/>.
  22. /// </summary>
  23. /// <param name="typeFormat">
  24. /// The formatting string representing the type
  25. /// where each generic type argument is represented as a
  26. /// formatting argument (e.g. <c>Dictionary&lt;{0},P1}&gt;</c>.
  27. /// </param>
  28. /// <param name="methodFormat">
  29. /// The formatting string representing the method (but not the declaring type).
  30. /// where each generic method argument is represented as a
  31. /// formatting argument (e.g. <c>ToArray&lt;{0}&gt;</c>.
  32. /// </param>
  33. /// <param name="parameterFormat">
  34. /// The formatting string representing the list of parameters, where each
  35. /// parameter is representing as a formatting argument
  36. /// (e.g. <c>{0}, {1}</c>).
  37. /// </param>
  38. /// <param name="methodIsGeneric">Indicates whether the method is generic.</param>
  39. /// <param name="typeIsGeneric">Indicates whether the type declaring the method is generic.</param>
  40. internal MethodFormatStrings(string typeFormat, bool typeIsGeneric,
  41. string methodFormat,
  42. bool methodIsGeneric,
  43. string parameterFormat)
  44. {
  45. this.typeFormat = typeFormat;
  46. this.methodFormat = methodFormat;
  47. this.parameterFormat = parameterFormat;
  48. this.typeIsGeneric = typeIsGeneric;
  49. this.methodIsGeneric = methodIsGeneric;
  50. }
  51. /// <summary>
  52. /// Gets a string representing a concrete method invocation.
  53. /// </summary>
  54. /// <param name="instance">Instance on which the method is invoked.</param>
  55. /// <param name="method">Invoked method.</param>
  56. /// <param name="invocationParameters">Concrete invocation parameters.</param>
  57. /// <returns>A representation of the method invocation.</returns>
  58. public string Format(
  59. object instance,
  60. MethodBase method,
  61. object[] invocationParameters)
  62. {
  63. string[] parts = new string[4]
  64. {
  65. typeIsGeneric
  66. ? Formatter.FormatString( this.typeFormat, method.DeclaringType.GetGenericArguments() )
  67. : this.typeFormat,
  68. methodIsGeneric
  69. ? Formatter.FormatString( this.methodFormat, method.GetGenericArguments() )
  70. : this.methodFormat,
  71. instance == null ? "" : string.Format( "{{{0}}}", instance ),
  72. Formatter.FormatString( this.parameterFormat, invocationParameters )
  73. };
  74. return string.Concat(parts);
  75. }
  76. }
  77. }