using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
namespace RestCake.Util
{
internal static class ReflectionHelper
{
///
/// This is used for the services "_help" pages, for generating documentation on service APIs.
/// It shouldn't be used for generating code, because the types may not always include their fully qualified names, and may not compile.
///
///
/// A string to put before the namespace
/// A string to put after the namespace
///
public static string GetTypeAsHtml(Type type, string nsBefore = "", string nsAfter = "")
{
if (type.IsGenericParameter)
return type.Name;
if (type.FullName != null && type.FullName.StartsWith("System.Nullable`1"))
return GetTypeAsHtml(type.GetGenericArguments()[0], nsBefore, nsAfter) + "?";
if (!type.IsGenericType)
{
if (type.Namespace == "System")
{
switch (type.Name)
{
case "Double":
case "String":
case "SByte":
case "Byte":
case "Char":
case "Decimal":
case "Object":
case "Void":
return type.Name.ToLower();
case "Boolean":
return "bool";
case "UInt16":
return "ushort";
case "UInt32":
return "uint";
case "UInt64":
return "ulong";
case "Int16":
return "short";
case "Int32":
return "int";
case "Int64":
return "long";
case "Single":
return "float";
default: return type.Name;
}
}
return nsBefore + type.Namespace + "." + nsAfter + type.Name;
}
var sb = new StringBuilder();
var name = type.Name;
var index = name.IndexOf("`");
if (type.Namespace == "System")
sb.AppendFormat("{0}", name.Substring(0, index));
else
sb.AppendFormat("{0}{1}", nsBefore + type.Namespace + "." + nsAfter, name.Substring(0, index));
sb.Append("<");
var first = true;
foreach (var arg in type.GetGenericArguments())
{
if (!first)
sb.Append(',');
sb.Append(GetTypeAsHtml(arg, nsBefore, nsAfter));
first = false;
}
sb.Append(">");
return sb.ToString();
}
}
}