コード例 #1
0
        public OperationResult(string errorMessage, EOperationResultLevel level)
        {
            Guard.IsNotNullOrWhiteSpace(errorMessage, nameof(errorMessage));
            GuardEx.IsValid(level, nameof(level));

            Level            = level;
            ExceptionMessage = errorMessage;
        }
コード例 #2
0
        public OperationRefResult(Exception e, EOperationResultLevel level)
        {
            Guard.IsNotNull(e, nameof(e));
            GuardEx.IsValid(level, nameof(level));

            Level            = level;
            Exception        = e;
            ExceptionMessage = e.Message;
        }
コード例 #3
0
        /// <summary>
        /// Returns whether <paramref name="element"/> is between <paramref name="lowerBound"/> and <paramref name="upperBound"/>.
        /// </summary>
        /// <typeparam name="T">The type of the values.</typeparam>
        /// <param name="element">The element to check.</param>
        /// <param name="lowerBound">The first boundary.</param>
        /// <param name="upperBound">The second boundary.</param>
        /// <param name="lowerInclusivity">The lower inclusivity.</param>
        /// <param name="upperInclusivity">The upper inclusivity.</param>
        /// <returns><see langword="true"/> if <paramref name="element"/> is between <paramref name="lowerBound"/> and <paramref name="upperBound"/>,
        /// <see langword="false"/> otherwise.</returns>
        /// <exception cref="InvalidOperationException">Thrown if <paramref name="lowerBound"/> equals <paramref name="upperBound"/> when <paramref name="lowerInclusivity"/> does not equal <paramref name="upperInclusivity"/>.</exception>
        /// <exception cref="ArgumentNullException"><paramref name="element"/> is <see langword="null"/></exception>
        /// <exception cref="Exception">A delegate callback throws an exception.</exception>
        public static bool IsBetween <T>([DisallowNull] this T element, [DisallowNull] T lowerBound, [DisallowNull] T upperBound,
                                         EInclusivity lowerInclusivity = EInclusivity.Inclusive, EInclusivity upperInclusivity = EInclusivity.Inclusive)
            where T : IComparable <T>
        {
            if (element is null)
            {
                // No Guard - missing class constraint
                ThrowHelper.ThrowArgumentNullException(nameof(element));
            }
            if (lowerBound is null)
            {
                // No Guard - missing class constraint
                ThrowHelper.ThrowArgumentNullException(nameof(lowerBound));
            }
            if (upperBound is null)
            {
                // No Guard - missing class constraint
                ThrowHelper.ThrowArgumentNullException(nameof(upperBound));
            }

            if (lowerBound.CompareTo(upperBound) > 0)
            {
                ThrowArgumentOrderException();
            }

            GuardEx.IsValid(lowerInclusivity, nameof(lowerInclusivity));
            GuardEx.IsValid(upperInclusivity, nameof(upperInclusivity));

            switch (lowerInclusivity)
            {
            case EInclusivity.Exclusive when upperInclusivity == EInclusivity.Exclusive:
                return(element.CompareTo(lowerBound) > 0 && element.CompareTo(upperBound) < 0);

            case EInclusivity.Inclusive when upperInclusivity == EInclusivity.Exclusive:
                if (lowerBound.CompareTo(upperBound) == 0)
                {
                    throw new InvalidOperationException($"{nameof(lowerBound)} cannot equal {nameof(upperBound)} when {nameof(lowerInclusivity)} does not equal {nameof(upperInclusivity)}.");
                }
                return(element.CompareTo(lowerBound) >= 0 && element.CompareTo(upperBound) < 0);

            case EInclusivity.Exclusive when upperInclusivity == EInclusivity.Inclusive:
                if (lowerBound.CompareTo(upperBound) == 0)
                {
                    throw new InvalidOperationException($"{nameof(lowerBound)} cannot equal {nameof(upperBound)} when {nameof(lowerInclusivity)} does not equal {nameof(upperInclusivity)}.");
                }
                return(element.CompareTo(lowerBound) > 0 && element.CompareTo(upperBound) <= 0);

            case EInclusivity.Inclusive when upperInclusivity == EInclusivity.Inclusive:
            default:
                return(element.CompareTo(lowerBound) >= 0 && element.CompareTo(upperBound) <= 0);
            }
        }
コード例 #4
0
        /// <summary>
        /// Returns whether this <see cref="VersionRange"/> contains the specified <see cref="Version"/>.
        /// </summary>
        /// <param name="version">The version to check is in the range of this <see cref="VersionRange"/>.</param>
        /// <param name="lowerInclusivity">The lower inclusivity.</param>
        /// <param name="upperInclusivity">The upper inclusivity.</param>
        public bool Contains(Version version, EInclusivity lowerInclusivity = EInclusivity.Inclusive, EInclusivity upperInclusivity = EInclusivity.Inclusive)
        {
            Guard.IsNotNull(version, nameof(version));
            GuardEx.IsValid(lowerInclusivity, nameof(lowerInclusivity));
            GuardEx.IsValid(upperInclusivity, nameof(upperInclusivity));

            return(lowerInclusivity switch
            {
                EInclusivity.Exclusive when upperInclusivity == EInclusivity.Exclusive => Minimum < version && version < Maximum,
                EInclusivity.Exclusive when upperInclusivity == EInclusivity.Inclusive => Minimum < version && version <= Maximum,
                EInclusivity.Inclusive when upperInclusivity == EInclusivity.Exclusive => Minimum <= version && version < Maximum,
                _ => Minimum <= version && version <= Maximum
            });
コード例 #5
0
        public static Exception?GetInnerExceptionOfType(this Exception?e, Type type, bool strictType = true)
        {
            Guard.IsNotNull(type, nameof(type));

            GuardEx.IsTypeOf <Exception>(type, nameof(type));

            if (e is null)
            {
                return(null);
            }

            foreach (var ex in GetInnerExceptions(e))
            {
                if (strictType ? ex.GetType() == type : ex.GetType().IsTypeOf(type))
                {
                    return(ex);
                }
            }

            return(null);
        }
コード例 #6
0
        /// <summary>
        /// Compresses the contents of <paramref name="file"/> into the archive.
        /// </summary>
        /// <param name="archive">The archive.</param>
        /// <param name="file">The file to compress.</param>
        /// <param name="destination">The destination in the archive.</param>
        /// <param name="compressionLevel">The compression level.</param>
        public static void CompressFile(this ZipArchive archive, string file, string?destination, CompressionLevel compressionLevel)
        {
            Guard.IsNotNull(archive, nameof(archive));
            Guard.IsNotNullOrWhiteSpace(file, nameof(file));
            GuardEx.IsValid(compressionLevel, nameof(compressionLevel));

            string desiredFileName = !string.IsNullOrWhiteSpace(destination) ? destination ! : Path.GetFileName(file);

            try
            {
                archive.CreateEntryFromFile(file, desiredFileName, compressionLevel);
            }
            catch (IOException e)
            {
                string tempFile = Path.GetTempFileName();
                try
                {
                    // The file is locked. Try copying to the temp directory, copy from there,
                    // then delete the copy.
                    File.Copy(file, tempFile, true);
                    archive.CreateEntryFromFile(tempFile, desiredFileName, compressionLevel);
                }
                catch (Exception)
                {
                    e.RestoreAndThrow();
                }
                finally
                {
                    try
                    {
                        File.Delete(tempFile);
                    }
                    catch
                    {
                        // Suppress
                    }
                }
            }
        }