| | | 1 | | using System.Collections.Concurrent; |
| | | 2 | | using System.Diagnostics.CodeAnalysis; |
| | | 3 | | using System.Text; |
| | | 4 | | using Elsa.Expressions.Models; |
| | | 5 | | |
| | | 6 | | // ReSharper disable once CheckNamespace |
| | | 7 | | namespace Elsa.Extensions; |
| | | 8 | | |
| | | 9 | | /// <summary> |
| | | 10 | | /// Adds extension methods to <see cref="Type"/>. |
| | | 11 | | /// </summary> |
| | | 12 | | public static class TypeExtensions |
| | | 13 | | { |
| | 1 | 14 | | private static readonly ConcurrentDictionary<Type, string> SimpleAssemblyQualifiedTypeNameCache = new(); |
| | | 15 | | |
| | | 16 | | /// <summary> |
| | | 17 | | /// Gets the assembly-qualified name of the type, without any version info etc. |
| | | 18 | | /// E.g. "System.String, System.Private.CoreLib" |
| | | 19 | | /// </summary> |
| | | 20 | | public static string GetSimpleAssemblyQualifiedName(this Type type) |
| | | 21 | | { |
| | 6202 | 22 | | if (type is null) throw new ArgumentNullException(nameof(type)); |
| | 6202 | 23 | | return SimpleAssemblyQualifiedTypeNameCache.GetOrAdd(type, GetSimplifiedName); |
| | | 24 | | } |
| | | 25 | | |
| | | 26 | | /// <summary> |
| | | 27 | | /// Returns the default value for the specified type. |
| | | 28 | | /// </summary> |
| | 0 | 29 | | public static object? GetDefaultValue(this Type type) => type.IsClass ? null : Activator.CreateInstance(type); |
| | | 30 | | |
| | | 31 | | /// <summary> |
| | | 32 | | /// Returns the element type of the specified type representing an array or generic enumerable. |
| | | 33 | | /// </summary> |
| | | 34 | | public static Type GetEnumerableElementType(this Type type) |
| | | 35 | | { |
| | 0 | 36 | | if (type.IsArray) |
| | 0 | 37 | | return type.GetElementType()!; |
| | | 38 | | |
| | 0 | 39 | | var elementType = FindIEnumerable(type); |
| | 0 | 40 | | return elementType == null ? type : elementType.GetGenericArguments()[0]; |
| | | 41 | | } |
| | | 42 | | |
| | | 43 | | /// <summary> |
| | | 44 | | /// Searches for the first implemented IEnumerable interface in the given type hierarchy, and returns the generic ty |
| | | 45 | | /// </summary> |
| | | 46 | | /// <param name="sequenceType">The type to search for the IEnumerable interface.</param> |
| | | 47 | | /// <returns>The generic type argument of the first implemented IEnumerable interface found in the type hierarchy, o |
| | | 48 | | [return: DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicParameterlessConstructor)] |
| | | 49 | | private static Type? FindIEnumerable([DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.Interfaces)] Type? s |
| | | 50 | | { |
| | 0 | 51 | | if (sequenceType == null || sequenceType == typeof(string)) |
| | 0 | 52 | | return null; |
| | | 53 | | |
| | 0 | 54 | | if (sequenceType.IsArray) |
| | 0 | 55 | | return typeof(IEnumerable<>).MakeGenericType(sequenceType.GetElementType()!); |
| | | 56 | | |
| | 0 | 57 | | if (sequenceType.IsGenericType) |
| | | 58 | | { |
| | 0 | 59 | | foreach (var arg in sequenceType.GetGenericArguments()) |
| | | 60 | | { |
| | 0 | 61 | | var enumerable = typeof(IEnumerable<>).MakeGenericType(arg); |
| | 0 | 62 | | if (enumerable.IsAssignableFrom(sequenceType)) |
| | 0 | 63 | | return enumerable; |
| | | 64 | | } |
| | | 65 | | } |
| | | 66 | | |
| | 0 | 67 | | var interfaces = sequenceType.GetInterfaces(); |
| | | 68 | | |
| | 0 | 69 | | if (interfaces is { Length: > 0 }) |
| | | 70 | | { |
| | 0 | 71 | | foreach (var interfaceType in interfaces) |
| | | 72 | | { |
| | 0 | 73 | | var enumerable = FindIEnumerable(interfaceType); |
| | 0 | 74 | | if (enumerable != null) return enumerable; |
| | | 75 | | } |
| | | 76 | | } |
| | 0 | 77 | | if (sequenceType.BaseType != null && sequenceType.BaseType != typeof(object)) |
| | 0 | 78 | | return FindIEnumerable(sequenceType.BaseType); |
| | | 79 | | |
| | 0 | 80 | | return null; |
| | | 81 | | } |
| | | 82 | | |
| | | 83 | | /// <summary> |
| | | 84 | | /// Gets a friendly type name for the specified type. |
| | | 85 | | /// </summary> |
| | | 86 | | public static string GetFriendlyTypeName(this Type type, Brackets brackets) |
| | | 87 | | { |
| | 0 | 88 | | if (!type.IsGenericType) |
| | 0 | 89 | | return type.FullName!; |
| | | 90 | | |
| | 0 | 91 | | var sb = new StringBuilder(); |
| | 0 | 92 | | sb.Append(type.Namespace); |
| | 0 | 93 | | sb.Append('.'); |
| | 0 | 94 | | sb.Append(type.Name[..type.Name.IndexOf('`')]); |
| | 0 | 95 | | sb.Append(brackets.Open); |
| | 0 | 96 | | var genericArgs = type.GetGenericArguments(); |
| | 0 | 97 | | for (var i = 0; i < genericArgs.Length; i++) |
| | | 98 | | { |
| | 0 | 99 | | if (i > 0) |
| | 0 | 100 | | sb.Append(", "); |
| | 0 | 101 | | sb.Append(GetFriendlyTypeName(genericArgs[i], brackets)); |
| | | 102 | | } |
| | | 103 | | |
| | 0 | 104 | | sb.Append(brackets.Close); |
| | 0 | 105 | | return sb.ToString(); |
| | | 106 | | } |
| | | 107 | | |
| | | 108 | | private static string GetSimplifiedName(Type type) |
| | | 109 | | { |
| | 88 | 110 | | var assemblyName = type.Assembly.GetName().Name; |
| | | 111 | | |
| | 88 | 112 | | if (type.IsGenericType) |
| | | 113 | | { |
| | 4 | 114 | | var genericTypeName = type.GetGenericTypeDefinition().FullName!; |
| | 4 | 115 | | var backtickIndex = genericTypeName.IndexOf('`'); |
| | 4 | 116 | | var typeNameWithoutArity = genericTypeName[..backtickIndex]; |
| | 4 | 117 | | var arity = genericTypeName[backtickIndex..]; |
| | | 118 | | |
| | 4 | 119 | | var genericArguments = type.GetGenericArguments(); |
| | 4 | 120 | | var simplifiedGenericArguments = genericArguments.Select(GetSimplifiedName); |
| | | 121 | | |
| | 4 | 122 | | return $"{typeNameWithoutArity}{arity}[[{string.Join("],[", simplifiedGenericArguments)}]], {assemblyName}"; |
| | | 123 | | } |
| | | 124 | | |
| | 84 | 125 | | var typeName = type.FullName; |
| | 84 | 126 | | return $"{typeName}, {assemblyName}"; |
| | | 127 | | } |
| | | 128 | | } |