Jump to content
Tuts 4 You

C# - get short name from type name ???


CodeExplorer

Recommended Posts

CodeExplorer

C# - get short name from type name ???
"Boolean" -> "bool"
I remember of seeing something like this but don't remember where...

 

Link to comment

AI said :

Type type = typeof(bool);
string shortName = type.Name; // shortName will be "Boolean"
string fullName = type.ToString(); // fullName will be "System.Boolean"

for the opposite gave this :

string fullName = "System.Boolean";
string shortName;
switch (fullName)
{
    case "System.Boolean":
        shortName = "bool";
        break;
    case "System.Int32":
        shortName = "int";
        break;
    case "System.String":
        shortName = "string";
        break;
    default:
        shortName = "Unknown type: " + fullName;
        break;
}

Console.WriteLine(shortName);

// OR //

Dictionary<string, string> typeNameMappings = new Dictionary<string, string>
{
    { "System.Boolean", "bool" },
    { "System.Int32", "int" },
    { "System.String", "string" },
    // Add more mappings as needed
};

string fullName = "System.Boolean";
string shortName;
if (typeNameMappings.TryGetValue(fullName, out shortName))
{
    Console.WriteLine(shortName); // Outputs "bool"
}
else
{
    Console.WriteLine("Unknown type: " + fullName);
}

 

Edited by whoknows
adding // OR //
  • Like 1
Link to comment
CodeExplorer

Finded in:
namespace ICSharpCode.NRefactory.MonoCSharp
        private string FormatType(Type t)
        {
            if (t == null)
            {
                return "";
            }
            string fullName = this.GetFullName(t);
            if (fullName == null)
            {
                return t.ToString();
            }
            if (!fullName.StartsWith("System."))
            {
                if (t.Namespace == this.t.Namespace)
                {
                    return t.Name;
                }
                return fullName;
            }
            else
            {
                if (t.HasElementType)
                {
                    Type elementType = t.GetElementType();
                    if (t.IsArray)
                    {
                        return this.FormatType(elementType) + " []";
                    }
                    if (t.IsPointer)
                    {
                        return this.FormatType(elementType) + " *";
                    }
                    if (t.IsByRef)
                    {
                        return "ref " + this.FormatType(elementType);
                    }
                }
                string key;
                switch (key = fullName)
                {
                case "System.Byte":
                    return "byte";
                case "System.SByte":
                    return "sbyte";
                case "System.Int16":
                    return "short";
                case "System.Int32":
                    return "int";
                case "System.Int64":
                    return "long";
                case "System.UInt16":
                    return "ushort";
                case "System.UInt32":
                    return "uint";
                case "System.UInt64":
                    return "ulong";
                case "System.Single":
                    return "float";
                case "System.Double":
                    return "double";
                case "System.Decimal":
                    return "decimal";
                case "System.Boolean":
                    return "bool";
                case "System.Char":
                    return "char";
                case "System.String":
                    return "string";
                case "System.Object":
                    return "object";
                case "System.Void":
                    return "void";
                }
                if (fullName.LastIndexOf(".") == 6)
                {
                    return fullName.Substring(7);
                }
                if (this.t.Namespace.StartsWith(t.Namespace + ".") || t.Namespace == this.t.Namespace)
                {
                    return fullName.Substring(t.Namespace.Length + 1);
                }
                return fullName;
            }
        }

 

  • Thanks 1
Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...