| | | 1 | | using System.Reflection; |
| | | 2 | | using Elsa.Workflows.Attributes; |
| | | 3 | | |
| | | 4 | | namespace Elsa.Workflows.Helpers; |
| | | 5 | | |
| | | 6 | | public static class ActivityTypeNameHelper |
| | | 7 | | { |
| | | 8 | | public static string? GenerateNamespace(Type activityType) |
| | | 9 | | { |
| | 38303 | 10 | | var activityAttr = activityType.GetCustomAttribute<ActivityAttribute>(); |
| | 38303 | 11 | | return activityAttr?.Namespace ?? activityType.Namespace; |
| | | 12 | | } |
| | | 13 | | |
| | | 14 | | public static string GenerateTypeName(Type type, string? ns) |
| | | 15 | | { |
| | 37813 | 16 | | var activityAttr = type.GetCustomAttribute<ActivityAttribute>(); |
| | | 17 | | string typeName; |
| | | 18 | | |
| | 37813 | 19 | | if (type.IsGenericType) |
| | | 20 | | { |
| | 149 | 21 | | var sanitizedTypeName = activityAttr?.Type ?? type.Name.Substring(0, type.Name.IndexOf("`", StringComparison |
| | 298 | 22 | | var genericArgs = type.GenericTypeArguments.Select(type1 => GenerateTypeName(type1, null)); |
| | 149 | 23 | | typeName = $"{sanitizedTypeName}<{string.Join(',', genericArgs)}>"; |
| | | 24 | | } |
| | | 25 | | else |
| | 37664 | 26 | | typeName = activityAttr?.Type ?? type.Name; |
| | | 27 | | |
| | 37813 | 28 | | return ns != null ? $"{ns}.{typeName}" : typeName; |
| | | 29 | | } |
| | | 30 | | |
| | 5792 | 31 | | public static string GenerateTypeName<T>() where T : IActivity => GenerateTypeName(typeof(T)); |
| | | 32 | | |
| | | 33 | | public static string GenerateTypeName(Type type) |
| | | 34 | | { |
| | 37664 | 35 | | var ns = GenerateNamespace(type); |
| | 37664 | 36 | | return GenerateTypeName(type, ns); |
| | | 37 | | } |
| | | 38 | | |
| | | 39 | | public static string? GetCategoryFromNamespace(string? ns) |
| | | 40 | | { |
| | 639 | 41 | | if (string.IsNullOrWhiteSpace(ns)) |
| | 0 | 42 | | return null; |
| | | 43 | | |
| | 639 | 44 | | var index = ns.LastIndexOf('.'); |
| | | 45 | | |
| | 639 | 46 | | return index < 0 ? ns : ns[(index + 1)..]; |
| | | 47 | | } |
| | | 48 | | } |