Пример #1
0
        public void IfInvalidEnum()
        {
            var theDateTimeKind = (DateTimeKind)999;

            Assert.Throws <InvalidEnumArgumentException>(() => DebugThrow.IfInvalidEnum(theDateTimeKind, "param"));

            DebugThrow.IfInvalidEnum(DateTimeKind.Utc, "param");
        }
Пример #2
0
        public static TValue?Get <TKey, TValue>(this IDictionary <TKey, TValue?> dictionary, TKey key)
            where TValue : class
        {
            DebugThrow.IfNull(dictionary, nameof(dictionary));

            dictionary.TryGetValue(key, out TValue? theValue);
            return(theValue);
        }
        /// <summary> Calls an <paramref name="action"/> for each element of the
        /// specified <see cref="System.Collections.Generic.IEnumerable{T}"/>.
        /// </summary>
        /// <typeparam name="T">Type</typeparam>
        /// <param name="source">Sequence</param>
        /// <param name="action">Action</param>
        public static void ForEach <T>(this IEnumerable <T> source, Action <T> action)
        {
            DebugThrow.IfNull(source, nameof(source));
            DebugThrow.IfNull(action, nameof(action));

            foreach (T theObject in source)
            {
                action(theObject);
            }
        }
        /// <summary>Flattens a recursive hierarchy of the specified <paramref name="source"/>.
        /// The <paramref name="recurseFunc"/> is used to access the children of the current
        /// element.
        /// </summary>
        /// <example><code><![CDATA[
        /// var theRoot = new Child("Father") {
        ///     Children = {
        ///         new Child("Son") {
        ///             Children = {
        ///                 new Child("Grandson 1"),
        ///                 new Child("Grandson 2")
        ///             }
        ///         }
        ///     }
        /// };
        ///
        /// var theFlattenedSeq = theRoot.ToEnumerable().FlattenRecursiveHierarchy(c => c.Children);
        /// ]]></code></example>
        /// <typeparam name="T">Type</typeparam>
        /// <param name="source">Sequence</param>
        /// <param name="recurseFunc">Used to access the children of the current element</param>
        /// <returns>Flattened sequence</returns>
        public static IEnumerable <T> FlattenRecursiveHierarchy <T>(this IEnumerable <T> source, Func <T, IEnumerable <T> > recurseFunc)
        {
            DebugThrow.IfNull(source, nameof(source));
            DebugThrow.IfNull(recurseFunc, nameof(recurseFunc));

            foreach (T theItem in source)
            {
                yield return(theItem);

                IEnumerable <T> theRecursiveEnumerable = recurseFunc(theItem);
                if (theRecursiveEnumerable != null)
                {
                    foreach (T theRecursiveItem in FlattenRecursiveHierarchy(theRecursiveEnumerable, recurseFunc))
                    {
                        yield return(theRecursiveItem);
                    }
                }
            }
        }
Пример #5
0
        public void If()
        {
            Assert.Throws <ArgumentException>(() => DebugThrow.If(true, "Wrong param value.", "param"));

            DebugThrow.If(false, "Wrong param value.", "param");
        }
Пример #6
0
        public void IfNull()
        {
            Assert.Throws <ArgumentNullException>(() => DebugThrow.IfNull((object?)null, "param"));

            DebugThrow.IfNull("not-null", "param");
        }