using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Reflection; namespace Bowin.Common.Cache { /// /// Set of 3 formatting string that, at runtime, represent a method and its /// parameters. /// [Serializable] internal class MethodFormatStrings { private readonly string typeFormat; private readonly string methodFormat; private readonly string parameterFormat; private readonly bool typeIsGeneric; private readonly bool methodIsGeneric; /// /// Initializes a new . /// /// /// The formatting string representing the type /// where each generic type argument is represented as a /// formatting argument (e.g. Dictionary<{0},P1}>. /// /// /// The formatting string representing the method (but not the declaring type). /// where each generic method argument is represented as a /// formatting argument (e.g. ToArray<{0}>. /// /// /// The formatting string representing the list of parameters, where each /// parameter is representing as a formatting argument /// (e.g. {0}, {1}). /// /// Indicates whether the method is generic. /// Indicates whether the type declaring the method is generic. internal MethodFormatStrings(string typeFormat, bool typeIsGeneric, string methodFormat, bool methodIsGeneric, string parameterFormat) { this.typeFormat = typeFormat; this.methodFormat = methodFormat; this.parameterFormat = parameterFormat; this.typeIsGeneric = typeIsGeneric; this.methodIsGeneric = methodIsGeneric; } /// /// Gets a string representing a concrete method invocation. /// /// Instance on which the method is invoked. /// Invoked method. /// Concrete invocation parameters. /// A representation of the method invocation. public string Format( object instance, MethodBase method, object[] invocationParameters) { string[] parts = new string[4] { typeIsGeneric ? Formatter.FormatString( this.typeFormat, method.DeclaringType.GetGenericArguments() ) : this.typeFormat, methodIsGeneric ? Formatter.FormatString( this.methodFormat, method.GetGenericArguments() ) : this.methodFormat, instance == null ? "" : string.Format( "{{{0}}}", instance ), Formatter.FormatString( this.parameterFormat, invocationParameters ) }; return string.Concat(parts); } } }