/// <summary>
        /// For the supplied IPublishedContent item, recurse all of its properties that return collections of IPublishedContent items (doing a safety check to ensure duplicates are not returned)
        /// Return an array of all distinct detached (by guid key) IPublishedContent items
        /// </summary>
        /// <param name="item">The IPublishedContent item to get all detached IPublihedContent items for</param>
        /// <param name="flatDetachedItems">The List into which to add the detached IPublishedContent items</param>
        /// <returns>All detached IPublishedContent items as a flat Array</returns>
        internal static IPublishedContent[] GetDetachedDescendants(this IPublishedContent item)
        {
            var enumerator = IPublishedContentExtensions.YieldDetachedDescendants(item).GetEnumerator();

            var items = new List <IPublishedContent>();
            var ok    = false;

            if (enumerator.MoveNext())
            {
                do
                {
                    var detachedItem = enumerator.Current;

                    ok = !items.Any(x => x.GetGuidKey() == detachedItem.GetGuidKey());

                    if (ok)
                    {
                        items.Add(detachedItem);
                    }
                    else
                    {
                        LogHelper.Warn(typeof(IPublishedContentExtensions), $"Detached IPublishedContent found with duplicate key: '{detachedItem.GetGuidKey()}'");
                    }
                } while (enumerator.MoveNext() && ok);
            }

            return(items.ToArray());
        }
Ejemplo n.º 2
0
        private static IEnumerable <IPublishedContent> YieldDetachedDescendants(this IPublishedContent item)
        {
            if (item != null)
            {
                foreach (var detachedProperty in IPublishedContentExtensions.YieldDetachedProperties(item))
                {
                    yield return(detachedProperty);

                    foreach (var detachedPropertyDescendant in IPublishedContentExtensions.YieldDetachedDescendants(detachedProperty))
                    {
                        yield return(detachedPropertyDescendant);
                    }
                }
            }
        }
 /// <summary>
 /// First a detached IPublishedContent item by key
 /// </summary>
 /// <param name="item">The IPublishedContent item to start search from</param>
 /// <returns>The IPublishedContent matching the supplied key, or null</returns>
 internal static IPublishedContent GetDetachedDescendant(this IPublishedContent item, Guid key)
 {
     return(IPublishedContentExtensions
            .YieldDetachedDescendants(item)
            .FirstOrDefault(x => x.GetGuidKey() == key));
 }