< Summary

Information
Class: Elsa.Common.Serialization.TypeTypeConverter
Assembly: Elsa.Common
File(s): /home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Common/Serialization/TypeTypeConverter.cs
Line coverage
0%
Covered lines: 0
Uncovered lines: 12
Coverable lines: 12
Total lines: 41
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 16
Branch coverage: 0%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
CanConvertFrom(...)0%620%
ConvertFrom(...)0%4260%
CanConvertTo(...)0%620%
ConvertTo(...)0%4260%

File(s)

/home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Common/Serialization/TypeTypeConverter.cs

#LineLine coverage
 1using System.ComponentModel;
 2using System.Globalization;
 3using JetBrains.Annotations;
 4
 5namespace Elsa.Common.Serialization;
 6
 7[PublicAPI]
 8public class TypeTypeConverter : TypeConverter
 9{
 10    public override bool CanConvertFrom(ITypeDescriptorContext? context, Type sourceType)
 11    {
 012        return sourceType == typeof(string) || base.CanConvertFrom(context, sourceType);
 13    }
 14
 15    public override object? ConvertFrom(ITypeDescriptorContext? context, CultureInfo? culture, object value)
 16    {
 017        if (value is string stringValue)
 18        {
 019            if (TypeAliasRegistry.GetType(stringValue) is { } type)
 020                return type;
 021            return Type.GetType(stringValue) ?? throw new InvalidOperationException($"Type '{stringValue}' not found.");
 22        }
 023        return base.ConvertFrom(context, culture, value);
 24    }
 25
 26    public override bool CanConvertTo(ITypeDescriptorContext? context, Type? destinationType)
 27    {
 028        return destinationType == typeof(string) || base.CanConvertTo(context, destinationType);
 29    }
 30
 31    public override object? ConvertTo(ITypeDescriptorContext? context, CultureInfo? culture, object? value, Type destina
 32    {
 033        if (destinationType == typeof(string) && value is Type type)
 34        {
 035            if (TypeAliasRegistry.TypeAliases.FirstOrDefault(x => x.Value == type).Key is { } alias)
 036                return alias;
 037            return type.AssemblyQualifiedName;
 38        }
 039        return base.ConvertTo(context, culture, value, destinationType);
 40    }
 41}