< Summary

Information
Class: Elsa.Extensions.TypeExtensions
Assembly: Elsa.Common
File(s): /home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Common/Extensions/TypeExtensions.cs
Line coverage
41%
Covered lines: 10
Uncovered lines: 14
Coverable lines: 24
Total lines: 88
Line coverage: 41.6%
Branch coverage
81%
Covered branches: 26
Total branches: 32
Branch coverage: 81.2%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.cctor()100%11100%
GetSimpleAssemblyQualifiedName(...)50%22100%
IsGenericType(...)100%22100%
IsNullableType(...)100%11100%
GetTypeOfNullable(...)100%11100%
IsCollectionType(...)0%2040%
MakeCollectionType(...)100%210%
GetCollectionElementType(...)100%210%
IsNumericType(...)100%2222100%
BuildSimplifiedName(...)50%3233.33%

File(s)

/home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Common/Extensions/TypeExtensions.cs

#LineLine coverage
 1using System.Collections.Concurrent;
 2
 3// ReSharper disable once CheckNamespace
 4namespace Elsa.Extensions;
 5
 6/// <summary>
 7/// Extends <see cref="Type"/> with additional methods.
 8/// </summary>
 9public static class TypeExtensions
 10{
 111    private static readonly ConcurrentDictionary<Type, string> SimpleAssemblyQualifiedTypeNameCache = new();
 12
 13    /// <summary>
 14    /// Gets the assembly-qualified name of the type, without version, culture, and public key token information.
 15    /// </summary>
 16    public static string GetSimpleAssemblyQualifiedName(this Type type)
 17    {
 469718        if (type is null) throw new ArgumentNullException(nameof(type));
 469719        return SimpleAssemblyQualifiedTypeNameCache.GetOrAdd(type, BuildSimplifiedName);
 20    }
 21
 22    /// <summary>
 23    /// Returns true of the type is generic, false otherwise.
 24    /// </summary>
 592825    public static bool IsGenericType(this Type type, Type genericType) => type.IsGenericType && type.GetGenericTypeDefin
 26
 27    /// <summary>
 28    /// Returns true of the type is nullable, false otherwise.
 29    /// </summary>
 592830    public static bool IsNullableType(this Type type) => type.IsGenericType(typeof(Nullable<>));
 31
 32    /// <summary>
 33    /// Returns the wrapped type of the specified nullable type.
 34    /// </summary>
 21635    public static Type GetTypeOfNullable(this Type type) => type.GenericTypeArguments[0];
 36
 37    /// <summary>
 38    /// Returns true if the specified type is a collection type, false otherwise.
 39    /// </summary>
 40    public static bool IsCollectionType(this Type type)
 41    {
 042        if (!type.IsGenericType)
 043            return false;
 44
 045        var elementType = type.GenericTypeArguments[0];
 046        var collectionType = typeof(ICollection<>).MakeGenericType(elementType);
 047        var listType = typeof(IList<>).MakeGenericType(elementType);
 048        return collectionType.IsAssignableFrom(type) || listType.IsAssignableFrom(type);
 49    }
 50
 51    /// <summary>
 52    /// Constructs a collection type from the specified type.
 53    /// </summary>
 054    public static Type MakeCollectionType(this Type type) => typeof(ICollection<>).MakeGenericType(type);
 55
 56    /// <summary>
 57    /// Returns the element type of the specified collection type.
 58    /// </summary>
 059    public static Type GetCollectionElementType(this Type type) => type.GenericTypeArguments[0];
 60
 61    /// <summary>
 62    /// Determines whether the specified type is a numeric type.
 63    /// </summary>
 64    /// <param name="type">The type to check.</param>
 65    /// <returns>True if the specified type is numeric, otherwise false.</returns>
 66    public static bool IsNumericType(this Type type)
 67    {
 1868        return type.IsPrimitive || type == typeof(decimal) || type == typeof(float) || type == typeof(double) || type ==
 69    }
 70
 71    private static string BuildSimplifiedName(Type type)
 72    {
 7673        var assemblyName = type.Assembly.GetName().Name;
 74
 7675        if (type.IsGenericType)
 76        {
 077            var genericTypeName = type.GetGenericTypeDefinition().FullName!;
 078            var backtickIndex = genericTypeName.IndexOf('`');
 079            var typeNameWithoutArity = genericTypeName[..backtickIndex];
 080            var arity = genericTypeName[backtickIndex..];
 081            var simplifiedGenericArguments = type.GetGenericArguments().Select(BuildSimplifiedName);
 82
 083            return $"{typeNameWithoutArity}{arity}[[{string.Join("],[", simplifiedGenericArguments)}]], {assemblyName}";
 84        }
 85
 7686        return $"{type.FullName}, {assemblyName}";
 87    }
 88}