| | | 1 | | using System.ComponentModel; |
| | | 2 | | using System.Globalization; |
| | | 3 | | using JetBrains.Annotations; |
| | | 4 | | |
| | | 5 | | namespace Elsa.Common.Serialization; |
| | | 6 | | |
| | | 7 | | [PublicAPI] |
| | | 8 | | public class TypeTypeConverter : TypeConverter |
| | | 9 | | { |
| | | 10 | | public override bool CanConvertFrom(ITypeDescriptorContext? context, Type sourceType) |
| | | 11 | | { |
| | 0 | 12 | | return sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); |
| | | 13 | | } |
| | | 14 | | |
| | | 15 | | public override object? ConvertFrom(ITypeDescriptorContext? context, CultureInfo? culture, object value) |
| | | 16 | | { |
| | 0 | 17 | | if (value is string stringValue) |
| | | 18 | | { |
| | 0 | 19 | | if (TypeAliasRegistry.GetType(stringValue) is { } type) |
| | 0 | 20 | | return type; |
| | 0 | 21 | | return Type.GetType(stringValue) ?? throw new InvalidOperationException($"Type '{stringValue}' not found."); |
| | | 22 | | } |
| | 0 | 23 | | return base.ConvertFrom(context, culture, value); |
| | | 24 | | } |
| | | 25 | | |
| | | 26 | | public override bool CanConvertTo(ITypeDescriptorContext? context, Type? destinationType) |
| | | 27 | | { |
| | 0 | 28 | | return destinationType == typeof(string) || base.CanConvertTo(context, destinationType); |
| | | 29 | | } |
| | | 30 | | |
| | | 31 | | public override object? ConvertTo(ITypeDescriptorContext? context, CultureInfo? culture, object? value, Type destina |
| | | 32 | | { |
| | 0 | 33 | | if (destinationType == typeof(string) && value is Type type) |
| | | 34 | | { |
| | 0 | 35 | | if (TypeAliasRegistry.TypeAliases.FirstOrDefault(x => x.Value == type).Key is { } alias) |
| | 0 | 36 | | return alias; |
| | 0 | 37 | | return type.AssemblyQualifiedName; |
| | | 38 | | } |
| | 0 | 39 | | return base.ConvertTo(context, culture, value, destinationType); |
| | | 40 | | } |
| | | 41 | | } |