Get Type As Html : HTML « Network « C# / C Sharp






Get Type As Html

 
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
    {


        /// <summary>
        /// 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.
        /// </summary>
        /// <param name="type"></param>
        /// <param name="nsBefore">A string to put before the namespace</param>
        /// <param name="nsAfter">A string to put after the namespace</param>
        /// <returns></returns>
        public static string GetTypeAsHtml(Type type, string nsBefore = "<span class='namespace'>", string nsAfter = "</span>")
        {
            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("&lt;");
            var first = true;
            foreach (var arg in type.GetGenericArguments())
            {
                if (!first)
                    sb.Append(',');
                sb.Append(GetTypeAsHtml(arg, nsBefore, nsAfter));
                first = false;
            }
            sb.Append("&gt;");
            return sb.ToString();
        }

    }
}

   
  








Related examples in the same category

1.Get Links From HTML
2.Parses the value information from any INPUT tag in an HTML string where the name="" attribute matched the tagID parameter
3.Html Utilities
4.Convert HTML To Text
5.Converts a FontUnit to a size for the HTML FONT tag
6.Strip HTML
7.Remove tags from a html string
8.Sanitize any potentially dangerous tags from the provided raw HTML input using a whitelist based approach
9.HTML-encodes a string and returns the encoded string.
10.Strips all HTML tags from the specified string.
11.Removes the HTML whitespace.
12.Array To Html Breaked String
13.Show Html Page in String with Process