Jump to content
View in the app

A better way to browse. Learn more.

Tuts 4 You

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

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

Featured Replies

Posted

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

 

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 //

  • Author

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;
            }
        }

 

Create an account or sign in to comment

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.