PageRenderTime 122ms CodeModel.GetById 60ms app.highlight 5ms RepoModel.GetById 55ms app.codeStats 0ms

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