Gets the typename full name. The type. The full typename of the type.
(this Type type)
| 16 | /// <param name="type">The type.</param> |
| 17 | /// <returns>The full typename of the type.</returns> |
| 18 | public static string GetTypeName(this Type type) |
| 19 | { |
| 20 | if (type.IsGenericType && type.IsConstructedGenericType) |
| 21 | { |
| 22 | // For generic types (eg. Dictionary) FullName returns generic parameter types with fully qualified name so simplify it manually |
| 23 | var sb = new StringBuilder(); |
| 24 | sb.Append(type.Namespace); |
| 25 | sb.Append('.'); |
| 26 | var parent = type.DeclaringType; |
| 27 | if (parent != null) |
| 28 | { |
| 29 | // Find outer types in which this one is nested |
| 30 | var count = 0; |
| 31 | while (parent != null) |
| 32 | { |
| 33 | count++; |
| 34 | parent = parent.DeclaringType; |
| 35 | } |
| 36 | var path = new Type[count]; |
| 37 | parent = type.DeclaringType; |
| 38 | count = 0; |
| 39 | while (parent != null) |
| 40 | { |
| 41 | path[count++] = parent; |
| 42 | parent = parent.DeclaringType; |
| 43 | } |
| 44 | |
| 45 | // Insert type nesting into typename path (from the back to front) |
| 46 | for (int i = count - 1; i >= 0; i--) |
| 47 | { |
| 48 | parent = path[i]; |
| 49 | sb.Append(parent.Name); |
| 50 | sb.Append('+'); |
| 51 | } |
| 52 | } |
| 53 | sb.Append(type.Name); |
| 54 | sb.Append('['); |
| 55 | var genericArgs = type.GetGenericArguments(); |
| 56 | for (var i = 0; i < genericArgs.Length; i++) |
| 57 | { |
| 58 | if (i != 0) |
| 59 | sb.Append(','); |
| 60 | sb.Append(genericArgs[i].GetTypeName()); |
| 61 | } |
| 62 | sb.Append(']'); |
| 63 | return sb.ToString(); |
| 64 | } |
| 65 | return type.FullName; |
| 66 | } |
| 67 | } |
| 68 | } |
no test coverage detected