| | | 1 | | using System.Collections.Concurrent; |
| | | 2 | | |
| | | 3 | | namespace Elsa.Api.Client.Extensions; |
| | | 4 | | |
| | | 5 | | /// <summary> |
| | | 6 | | /// Adds extension methods to <see cref="Type"/>. |
| | | 7 | | /// </summary> |
| | | 8 | | public static class TypeExtensions |
| | | 9 | | { |
| | 0 | 10 | | private static readonly ConcurrentDictionary<Type, string> SimpleAssemblyQualifiedTypeNameCache = new(); |
| | | 11 | | |
| | | 12 | | /// <summary> |
| | | 13 | | /// Gets the assembly-qualified name of the type, without any version info etc. |
| | | 14 | | /// E.g. "System.String, System.Private.CoreLib" |
| | | 15 | | /// </summary> |
| | | 16 | | public static string GetSimpleAssemblyQualifiedName(this Type type) |
| | | 17 | | { |
| | 0 | 18 | | if (type is null) throw new ArgumentNullException(nameof(type)); |
| | 0 | 19 | | return SimpleAssemblyQualifiedTypeNameCache.GetOrAdd(type, BuildSimplifiedName); |
| | | 20 | | } |
| | | 21 | | |
| | | 22 | | /// <summary> |
| | | 23 | | /// Returns the default value for the specified type. |
| | | 24 | | /// </summary> |
| | 0 | 25 | | public static object? GetDefaultValue(this Type type) => type.IsClass ? null : Activator.CreateInstance(type); |
| | | 26 | | |
| | | 27 | | /// <summary> |
| | | 28 | | /// Returns the element type of the specified type representing an array or generic enumerable. |
| | | 29 | | /// </summary> |
| | | 30 | | public static Type GetEnumerableElementType(this Type type) |
| | | 31 | | { |
| | 0 | 32 | | if (type.IsArray) |
| | 0 | 33 | | return type.GetElementType()!; |
| | | 34 | | |
| | 0 | 35 | | var elementType = FindIEnumerable(type); |
| | 0 | 36 | | return elementType == null ? type : elementType.GetGenericArguments()[0]; |
| | | 37 | | } |
| | | 38 | | |
| | | 39 | | /// <summary> |
| | | 40 | | /// Searches for the first implemented IEnumerable interface in the given type hierarchy, and returns the generic ty |
| | | 41 | | /// </summary> |
| | | 42 | | /// <param name="sequenceType">The type to search for the IEnumerable interface.</param> |
| | | 43 | | /// <returns>The generic type argument of the first implemented IEnumerable interface found in the type hierarchy, o |
| | | 44 | | private static Type? FindIEnumerable(Type? sequenceType) |
| | | 45 | | { |
| | 0 | 46 | | if (sequenceType == null || sequenceType == typeof(string)) |
| | 0 | 47 | | return null; |
| | | 48 | | |
| | 0 | 49 | | if (sequenceType.IsArray) |
| | 0 | 50 | | return typeof(IEnumerable<>).MakeGenericType(sequenceType.GetElementType()!); |
| | | 51 | | |
| | 0 | 52 | | if (sequenceType.IsGenericType) |
| | | 53 | | { |
| | 0 | 54 | | foreach (var arg in sequenceType.GetGenericArguments()) |
| | | 55 | | { |
| | 0 | 56 | | var enumerable = typeof(IEnumerable<>).MakeGenericType(arg); |
| | 0 | 57 | | if (enumerable.IsAssignableFrom(sequenceType)) |
| | 0 | 58 | | return enumerable; |
| | | 59 | | } |
| | | 60 | | } |
| | | 61 | | |
| | 0 | 62 | | var interfaces = sequenceType.GetInterfaces(); |
| | | 63 | | |
| | 0 | 64 | | if (interfaces is { Length: > 0 }) |
| | | 65 | | { |
| | 0 | 66 | | foreach (var interfaceType in interfaces) |
| | | 67 | | { |
| | 0 | 68 | | var enumerable = FindIEnumerable(interfaceType); |
| | 0 | 69 | | if (enumerable != null) return enumerable; |
| | | 70 | | } |
| | | 71 | | } |
| | 0 | 72 | | if (sequenceType.BaseType != null && sequenceType.BaseType != typeof(object)) |
| | 0 | 73 | | return FindIEnumerable(sequenceType.BaseType); |
| | | 74 | | |
| | 0 | 75 | | return null; |
| | | 76 | | } |
| | | 77 | | |
| | | 78 | | private static string BuildSimplifiedName(Type type) |
| | | 79 | | { |
| | 0 | 80 | | var assemblyName = type.Assembly.GetName().Name; |
| | | 81 | | |
| | 0 | 82 | | if (type.IsGenericType) |
| | | 83 | | { |
| | 0 | 84 | | var genericTypeName = type.GetGenericTypeDefinition().FullName!; |
| | 0 | 85 | | var backtickIndex = genericTypeName.IndexOf('`'); |
| | 0 | 86 | | var typeNameWithoutArity = genericTypeName[..backtickIndex]; |
| | 0 | 87 | | var arity = genericTypeName[backtickIndex..]; |
| | | 88 | | |
| | 0 | 89 | | var genericArguments = type.GetGenericArguments(); |
| | 0 | 90 | | var simplifiedGenericArguments = genericArguments.Select(BuildSimplifiedName); |
| | | 91 | | |
| | 0 | 92 | | return $"{typeNameWithoutArity}{arity}[[{string.Join("],[", simplifiedGenericArguments)}]], {assemblyName}"; |
| | | 93 | | } |
| | | 94 | | |
| | 0 | 95 | | var typeName = type.FullName; |
| | 0 | 96 | | return $"{typeName}, {assemblyName}"; |
| | | 97 | | } |
| | | 98 | | } |