Пример #1
0
        private static IEnumerable <Entity> GetEntities(IAggregateRoot o)
        {
            IEnumerable <object> Deconstruct(object obj)
            {
                var props = obj.GetType()
                            .Fields(Flags.InstanceAnyVisibility)
                            .Where(fp => typeof(Entity).IsAssignableFrom(fp.Type()) || typeof(IEnumerable).IsAssignableFrom(fp.Type()) && typeof(string) != fp.Type());
                var instances = props.Select(p => o.TryGetValue(p.Name, Flags.InstanceAnyVisibility))
                                .Where(i => i != null);

                return(instances);
            }

            var queue = new ConcurrentQueue <object>(Deconstruct(o));

            while (queue.TryDequeue(out var obj))
            {
                if (obj is IEnumerable enumerable)
                {
                    foreach (var item in enumerable)
                    {
                        queue.Enqueue(item);
                    }
                }
                else
                {
                    if (obj is Entity entity)
                    {
                        yield return(entity);
                    }

                    foreach (var d in Deconstruct(obj))
                    {
                        queue.Enqueue(d);
                    }
                }
            }
        }