123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Reflection;
- namespace Bowin.Common.Cache
- {
- /// <summary>
- /// Provides formatting string representing types, methods and fields. The
- /// formatting strings may contain arguments like <c>{0}</c>
- /// filled at runtime with generic parameters or method arguments.
- /// </summary>
- internal static class Formatter
- {
- /// <summary>
- /// Gets a formatting string representing a <see cref="Type"/>.
- /// </summary>
- /// <param name="type">A <see cref="Type"/>.</param>
- /// <returns>A formatting string representing the type
- /// where each generic type argument is represented as a
- /// formatting argument (e.g. <c>Dictionary<{0},P1}></c>.
- /// </returns>
- public static string GettypeFormatString(Type type)
- {
- StringBuilder stringBuilder = new StringBuilder();
- // Build the format string for the declaring type.
- stringBuilder.Append(type.FullName);
- if (type.IsGenericTypeDefinition)
- {
- stringBuilder.Append("<");
- for (int i = 0; i < type.GetGenericArguments().Length; i++)
- {
- if (i > 0)
- stringBuilder.Append(", ");
- stringBuilder.AppendFormat("{{{0}}}", i);
- }
- stringBuilder.Append(">");
- }
- return stringBuilder.ToString();
- }
- /// <summary>
- /// Gets the formatting strings representing a method.
- /// </summary>
- /// <param name="method">A <see cref="MethodBase"/>.</param>
- /// <returns></returns>
- public static MethodFormatStrings GetMethodFormatStrings(MethodBase method)
- {
- string typeFormat;
- bool typeIsGeneric;
- string methodFormat;
- bool methodIsGeneric;
- string parameterFormat;
- StringBuilder stringBuilder = new StringBuilder();
- typeFormat = GettypeFormatString(method.DeclaringType);
- typeIsGeneric = method.DeclaringType.IsGenericTypeDefinition;
- // Build the format string for the method name.
- stringBuilder.Length = 0;
- stringBuilder.Append("::");
- stringBuilder.Append(method.Name);
- if (method.IsGenericMethodDefinition)
- {
- methodIsGeneric = true;
- stringBuilder.Append("<");
- for (int i = 0; i < method.GetGenericArguments().Length; i++)
- {
- if (i > 0)
- stringBuilder.Append(", ");
- stringBuilder.AppendFormat("{{{0}}}", i);
- }
- stringBuilder.Append(">");
- }
- else
- {
- methodIsGeneric = false;
- }
- methodFormat = stringBuilder.ToString();
- // Build the format string for parameters.
- stringBuilder.Length = 0;
- ParameterInfo[] parameters = method.GetParameters();
- stringBuilder.Append("(");
- for (int i = 0; i < parameters.Length; i++)
- {
- if (i > 0)
- {
- stringBuilder.Append(", ");
- }
- stringBuilder.Append("{{{");
- stringBuilder.Append(i);
- stringBuilder.Append("}}}");
- }
- stringBuilder.Append(")");
- parameterFormat = stringBuilder.ToString();
- return new MethodFormatStrings(typeFormat, typeIsGeneric, methodFormat, methodIsGeneric, parameterFormat);
- }
- /// <summary>
- /// Pads a string with a space, if not empty and not yet padded.
- /// </summary>
- /// <param name="prefix">A string.</param>
- /// <returns>A padded string.</returns>
- public static string NormalizePrefix(string prefix)
- {
- if (string.IsNullOrEmpty(prefix))
- {
- return "";
- }
- else if (prefix.EndsWith(" "))
- {
- return prefix;
- }
- else
- {
- return prefix + " ";
- }
- }
- public static string FormatString(string format, params object[] args)
- {
- if (args == null)
- return format;
- else
- return string.Format(format, args);
- }
- }
- }
|