| | | 1 | | using System.Reflection; |
| | | 2 | | using Elsa.Workflows.Models; |
| | | 3 | | |
| | | 4 | | namespace Elsa.Workflows.PortResolvers; |
| | | 5 | | |
| | | 6 | | /// <summary> |
| | | 7 | | /// Returns a list of outbound activities for a given activity by reflecting over its public properties matching <see cr |
| | | 8 | | /// </summary> |
| | | 9 | | public class PropertyBasedActivityResolver : IActivityResolver |
| | | 10 | | { |
| | | 11 | | /// <inheritdoc /> |
| | 851 | 12 | | public int Priority => -1; |
| | | 13 | | |
| | | 14 | | /// <inheritdoc /> |
| | 16645 | 15 | | public bool GetSupportsActivity(IActivity activity) => true; |
| | | 16 | | |
| | | 17 | | /// <inheritdoc /> |
| | | 18 | | public ValueTask<IEnumerable<ActivityPort>> GetActivityPortsAsync(IActivity activity, CancellationToken cancellation |
| | | 19 | | { |
| | 16645 | 20 | | return new(GetActivityPortsInternal(activity)); |
| | | 21 | | } |
| | | 22 | | |
| | | 23 | | private static IEnumerable<ActivityPort> GetActivityPortsInternal(IActivity activity) |
| | | 24 | | { |
| | 16645 | 25 | | var activityType = activity.GetType(); |
| | | 26 | | |
| | 16645 | 27 | | var ports = |
| | 16645 | 28 | | from prop in activityType.GetProperties(BindingFlags.Public | BindingFlags.Instance) |
| | 299048 | 29 | | where typeof(IActivity).IsAssignableFrom(prop.PropertyType) || typeof(IEnumerable<IActivity>).IsAssignableFr |
| | 11914 | 30 | | let value = prop.GetValue(activity) |
| | 11914 | 31 | | let isCollection = GetPropertyIsCollection(prop.PropertyType) |
| | 11914 | 32 | | let portName = prop.Name |
| | 11914 | 33 | | where value != null |
| | 25860 | 34 | | select isCollection ? ActivityPort.FromActivities((IEnumerable<IActivity>)value, portName) : ActivityPort.Fr |
| | | 35 | | |
| | 16645 | 36 | | return ports.ToList(); |
| | | 37 | | } |
| | | 38 | | |
| | 11914 | 39 | | private static bool GetPropertyIsCollection(Type propertyType) => typeof(IEnumerable<IActivity>).IsAssignableFrom(pr |
| | | 40 | | } |