-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathOperationExtensions.cs
More file actions
33 lines (27 loc) · 1.13 KB
/
OperationExtensions.cs
File metadata and controls
33 lines (27 loc) · 1.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
using System.Collections;
using System.Linq;
using System.Reflection;
using VirtoCommerce.CoreModule.Core.Common;
namespace VirtoCommerce.OrdersModule.Core.Extensions;
public static class OperationExtensions
{
public static void FillChildOperations(this IOperation operation)
{
var properties = operation.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance);
var childOperations = properties
.Where(x => x.PropertyType.GetInterface(nameof(IOperation)) != null)
.Select(x => (IOperation)x.GetValue(operation)).Where(x => x != null)
.ToList();
// Handle collections
var collections = properties
.Where(x => x.Name != nameof(operation.ChildrenOperations) && x.GetIndexParameters().Length == 0)
.Select(x => x.GetValue(operation, index: null))
.Where(x => x is IEnumerable and not string)
.Cast<IEnumerable>();
foreach (var collection in collections)
{
childOperations.AddRange(collection.OfType<IOperation>());
}
operation.ChildrenOperations = childOperations;
}
}