コード例 #1
0
        public static string GetHumanReadableTypeName(TypeReference type)
        {
            if (type.IsPointer)
            {
                return(GetHumanReadableTypeName(type.GetElementType()) + "*");
            }

            string name = type.CSFullName();

            switch (type.MetadataType)
            {
            case MetadataType.Void:
                name = "void";
                break;

            case MetadataType.Boolean:
                name = "bool";
                break;

            case MetadataType.Char:
                name = "char";
                break;

            case MetadataType.SByte:
                name = "sbyte";
                break;

            case MetadataType.Byte:
                name = "byte";
                break;

            case MetadataType.Int16:
                name = "short";
                break;

            case MetadataType.UInt16:
                name = "ushort";
                break;

            case MetadataType.Int32:
                name = "int";
                break;

            case MetadataType.UInt32:
                name = "uint";
                break;

            case MetadataType.Int64:
                name = "long";
                break;

            case MetadataType.UInt64:
                name = "ulong";
                break;

            case MetadataType.Single:
                name = "float";
                break;

            case MetadataType.Double:
                name = "double";
                break;
            }

            if (type.Resolve().HasAttribute("CsName"))
            {
                return(type.Resolve().GetValueOfAttribute("CsName"));
            }

            /*if (Nullable.GetUnderlyingType(type) != null && simplify)
             * {
             *  return GetHumanReadableTypeName(Nullable.GetUnderlyingType(type), simplify, expose_template) + "?";
             * }
             * else*/
            if (type.IsGenericInstance)
            {
                var    fullName = type.CSFullName();
                string typeName = fullName.Substring(0, fullName.IndexOf('`'));

                typeName = string.Format("{0}<{1}>", typeName,
                                         ((GenericInstanceType)type).GenericArguments.Count == 0 ? "" : ((GenericInstanceType)type).GenericArguments
                                         .Select(x => GetHumanReadableTypeName(x))
                                         .Aggregate((x, y) => string.Format("{0}, {1}", x, y)));

                return(typeName);
            }

            name = name.Replace('/', '.');

            if (name.StartsWith("System"))
            {
                name = "global::" + name;
            }

            if (type.IsGenericParameter)
            {
                return(name);
            }

            return(name);
        }