Exemple #1
0
        /// <summary>
        /// Discovers the siblings of the specified sortable object.
        /// </summary>
        static async Task <IEnumerable <ISortable> > FindSiblings(ISortable item)
        {
            var getSiblingsMethod = item.GetType().GetMethod("GetSiblings", BindingFlags.Public | BindingFlags.Instance);

            var isAcceptable = true;

            if (getSiblingsMethod == null)
            {
                isAcceptable = false;
            }
            else if (getSiblingsMethod.GetParameters().Any())
            {
                isAcceptable = false;
            }
            else if (!getSiblingsMethod.ReturnType.Implements(typeof(IEnumerable)))
            {
                isAcceptable = false;
            }

            IEnumerable <ISortable> result;

            if (!isAcceptable)
            {
                result = (await Database.Of(item.GetType()).GetList()).Cast <ISortable>();
            }
            else
            {
                var list = new List <ISortable>();

                try
                {
                    foreach (ISortable element in getSiblingsMethod.Invoke(item, null) as IEnumerable)
                    {
                        list.Add(element);
                    }
                }
                catch (Exception ex)
                {
                    throw new Exception($"Services.Sorter Could not process the GetSiblings method from the {item.GetType().Name} instance.", ex);
                }

                result = list;
            }

            return(result.OrderBy(i => i.Order).ToList());
        }
Exemple #2
0
        /// <summary>
        /// Discovers the siblings of the specified sortable object.
        /// </summary>
        static async Task <IEnumerable <ISortable> > FindSiblings(ISortable item)
        {
            var getSiblingsMethod = item.GetType().GetMethod("GetSiblings", BindingFlags.Public | BindingFlags.Instance);

            IEnumerable <ISortable> result;

            if (!IsAcceptable(getSiblingsMethod))
            {
                result = (await Database.Of(item.GetType()).GetList()).Cast <ISortable>();
            }
            else
            {
                var list = new List <ISortable>();

                try
                {
                    IEnumerable collection;

                    if (getSiblingsMethod.ReturnType.IsA <Task>())
                    {
                        collection = await(dynamic) getSiblingsMethod.Invoke(item, null) as IEnumerable <ISortable>;
                    }
                    else
                    {
                        collection = getSiblingsMethod.Invoke(item, null) as IEnumerable;
                    }

                    foreach (ISortable element in collection)
                    {
                        list.Add(element);
                    }
                }
                catch (Exception ex)
                {
                    throw new Exception($"Services.Sorter Could not process the GetSiblings method from the {item.GetType().Name} instance.", ex);
                }

                result = list;
            }

            return(result.OrderBy(i => i.Order).ToList());
        }