/src/LinFu.AOP/InvocationInfo.cs

http://github.com/philiplaureano/LinFu · C# · 139 lines · 58 code · 20 blank · 61 comment · 3 complexity · ab0f457025bc8f064218c3384b12ec93 MD5 · raw file

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.IO;
  5. using System.Reflection;
  6. using LinFu.AOP.Interfaces;
  7. namespace LinFu.AOP.Cecil
  8. {
  9. /// <summary>
  10. /// Represents the information associated with
  11. /// a single method call.
  12. /// </summary>
  13. [Serializable]
  14. public class InvocationInfo : IInvocationInfo
  15. {
  16. /// <summary>
  17. /// Initializes the <see cref="InvocationInfo" /> instance.
  18. /// </summary>
  19. /// <param name="target">The target instance currently being called.</param>
  20. /// <param name="targetMethod">The method currently being called.</param>
  21. /// <param name="stackTrace"> The <see cref="StackTrace" /> associated with the method call when the call was made.</param>
  22. /// <param name="parameterTypes">The parameter types for the current target method.</param>
  23. /// <param name="typeArguments">
  24. /// If the <see cref="TargetMethod" /> method is a generic method,
  25. /// this will hold the generic type arguments used to construct the
  26. /// method.
  27. /// </param>
  28. /// <param name="returnType">The return type of the target method.</param>
  29. /// <param name="arguments">The arguments used in the method call.</param>
  30. public InvocationInfo(object target, MethodBase targetMethod,
  31. StackTrace stackTrace, Type[] parameterTypes,
  32. Type[] typeArguments, Type returnType, object[] arguments)
  33. {
  34. Target = target;
  35. TargetMethod = targetMethod;
  36. StackTrace = stackTrace;
  37. ParameterTypes = parameterTypes;
  38. TypeArguments = typeArguments;
  39. Arguments = arguments;
  40. ReturnType = returnType;
  41. }
  42. /// <summary>
  43. /// This is the actual calling method that invoked the <see cref="TargetMethod" />.
  44. /// </summary>
  45. public MethodBase CallingMethod
  46. {
  47. get
  48. {
  49. var frame = StackTrace.GetFrame(0);
  50. return frame.GetMethod();
  51. }
  52. }
  53. /// <summary>
  54. /// The target instance currently being called.
  55. /// </summary>
  56. /// <remarks>This typically is a reference to a proxy object.</remarks>
  57. public object Target { get; }
  58. /// <summary>
  59. /// The method currently being called.
  60. /// </summary>
  61. public MethodBase TargetMethod { get; }
  62. /// <summary>
  63. /// The <see cref="StackTrace" /> associated
  64. /// with the method call when the call was made.
  65. /// </summary>
  66. public StackTrace StackTrace { get; }
  67. /// <summary>
  68. /// The return type of the <see cref="TargetMethod" />.
  69. /// </summary>
  70. public Type ReturnType { get; }
  71. /// <summary>
  72. /// The parameter types for the current target method.
  73. /// </summary>
  74. /// <remarks>
  75. /// <para>
  76. /// This could be very useful in cases where the actual target method
  77. /// is based on a generic type definition. In such cases,
  78. /// the <see cref="IInvocationInfo" /> instance needs to be able
  79. /// to describe the actual parameter types being used by the
  80. /// current generic type instantiation. This property helps
  81. /// users determine which parameter types are actually being used
  82. /// at the time of the method call.
  83. /// </para>
  84. /// </remarks>
  85. public Type[] ParameterTypes { get; }
  86. /// <summary>
  87. /// If the <see cref="TargetMethod" /> method is a generic method,
  88. /// this will hold the generic type arguments used to construct the
  89. /// method.
  90. /// </summary>
  91. public Type[] TypeArguments { get; }
  92. /// <summary>
  93. /// The arguments used in the method call.
  94. /// </summary>
  95. public object[] Arguments { get; }
  96. /// <summary>
  97. /// Returns a string that represents the current object.
  98. /// </summary>
  99. /// <returns>A string that represents the current object.</returns>
  100. public override string ToString()
  101. {
  102. var writer = new StringWriter();
  103. var targetMethod = TargetMethod;
  104. writer.Write("{0}.{1}(", targetMethod.DeclaringType, targetMethod.Name);
  105. var arguments = new Queue<object>(Arguments);
  106. while (arguments.Count > 0)
  107. {
  108. var argument = arguments.Dequeue();
  109. if (argument is string)
  110. argument = string.Format("\"{0}\"", argument);
  111. writer.Write(argument);
  112. if (arguments.Count > 0)
  113. writer.Write(", ");
  114. }
  115. writer.WriteLine(")");
  116. return writer.ToString();
  117. }
  118. }
  119. }