C# Generic Type and Aliased Type to string

C# code, that uses reflection, for translating a type to a pretty class name. Solving the problem that Type.Name returns a really ugly string:

  • typeof(int?).ToString() = System.Nullable`1[System.Int32]
  • typeof(List<string>).ToString() = System.Collections.Generic.List`1[System.String]
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace IdentityAudit.Utilities
{
    public static class TypePrettyName
    {
        public static bool IsSubclassOfRawGeneric(Type generic, Type toCheck)
        {
            while (toCheck != typeof(object) && toCheck != null)
            {
                var cur = toCheck.IsGenericType ? toCheck.GetGenericTypeDefinition() : toCheck;
                if (generic == cur)
                {
                    return true;
                }
                toCheck = toCheck.BaseType;
            }
            return false;
        }

        public static string GetTypePrettyName(Type type)
        {
            if (type.IsGenericType)
            {
                if (IsSubclassOfRawGeneric(typeof(Nullable<>), type))
                {
                    return GetTypePrettyName(type.GetGenericArguments()[0]) + "?";
                }

                string typeName = type.Name.Remove(type.Name.IndexOf('`'));
                string args =
                    string.Join(
                        ",",
                        type.GetGenericArguments().Select(arg => GetTypePrettyName(arg)));
                return typeName + "            }
            else
            {
                return SimpleTypeNameUseAlias(type);
            }
        }

        public static string SimpleTypeNameUseAlias(Type type)
        {
            switch (type.Name)
            {
                case "String": return "string";
                case "Object": return "object";
                case "Boolean": return "bool";
                case "Byte": return "bool";
                case "SByte": return "sbyte";
                case "Int16": return "short";
                case "UInt16": return "ushort";
                case "Int32": return "int";
                case "UInt32": return "uint";
                case "Int64": return "long";
                case "UInt64": return "ulong";
                case "Single": return "float";
                case "Double": return "double";
                case "Decimal": return "decimal";
                case "Char": return "char";
                default:
                    return type.Name;
            }
        }
    }
}

About mfagerlund
Writes code in my sleep - and sometimes it even compiles!

One Response to C# Generic Type and Aliased Type to string

  1. LurkersRUs says:

    tags are broken on line 38 return typeName + ” }

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: