Gets the typename name for UI. The type. The display of the type.
(this Type type)
| 39 | /// <param name="type">The type.</param> |
| 40 | /// <returns>The display of the type.</returns> |
| 41 | public static string GetTypeDisplayName(this Type type) |
| 42 | { |
| 43 | // Special display for in-built basic types |
| 44 | if (type == null) |
| 45 | return "Null"; |
| 46 | if (type == typeof(bool)) |
| 47 | return "Bool"; |
| 48 | if (type == typeof(float)) |
| 49 | return "Float"; |
| 50 | if (type == typeof(int)) |
| 51 | return "Int"; |
| 52 | if (type == typeof(uint)) |
| 53 | return "Uint"; |
| 54 | |
| 55 | // For generic types (eg. Dictionary) Name returns generic parameter types with fully qualified name so simplify it manually |
| 56 | if (type.IsGenericType) |
| 57 | { |
| 58 | var sb = new StringBuilder(); |
| 59 | var name = type.Name; |
| 60 | var idx = name.IndexOf('`'); |
| 61 | sb.Append(idx != -1 ? name.Substring(0, idx) : name); |
| 62 | sb.Append('<'); |
| 63 | var genericArgs = type.GetGenericArguments(); |
| 64 | for (var i = 0; i < genericArgs.Length; i++) |
| 65 | { |
| 66 | if (i != 0) |
| 67 | sb.Append(", "); |
| 68 | sb.Append(genericArgs[i].GetTypeDisplayName()); |
| 69 | } |
| 70 | sb.Append('>'); |
| 71 | return sb.ToString(); |
| 72 | } |
| 73 | |
| 74 | // Default name |
| 75 | return type.Name; |
| 76 | } |
| 77 | |
| 78 | /// <summary> |
| 79 | /// Gets the default value for the given type (can be value type or reference type). |
no test coverage detected