/// <summary>
        /// Converts IDictionary to delimited string.
        /// </summary>
        /// <param name="list">The list.</param>
        /// <param name="delimiter">The delimiter.</param>
        /// <returns>System.String.</returns>
        internal static string ToDelimitedString([NotNull] this IDictionary list, char delimiter = ',')
        {
            if (string.IsNullOrEmpty(delimiter.ToString()))
            {
                ExceptionThrower.ThrowArgumentNullException(nameof(delimiter));
            }

            if (list.Count() == 0)
            {
                return(string.Empty);
            }

            var sb = new StringBuilder();

            foreach (DictionaryEntry item in list)
            {
                if (sb.Length > 0)
                {
                    _ = sb.Append(delimiter.ToString(CultureInfo.CurrentCulture));
                }

                _ = sb.Append($"{item.Key}: {item.Value}".ToString(CultureInfo.CurrentCulture));
            }

            return(sb.ToString());
        }
Exemple #2
0
        public static T ArgumentDefined <T>(this T input, string errorMessage = "", [CallerArgumentExpression("input")] string paramName = "") where T : Enum
        {
            var isValid = Enum.IsDefined(input.GetType(), input);

            if (isValid is false)
            {
                ExceptionThrower.ThrowArgumentOutOfRangeException(CreateExceptionMessage(errorMessage, Resources.ErrorEnumNotDefined), paramName);
            }

            return(input);
        }
Exemple #3
0
        public static bool CheckEquals(this Type input, Type expectedType, bool throwException = false, string errorMessage = "")
        {
            var isValid = (input is not null) && (expectedType != null) && (input == expectedType);

            if (isValid is false && throwException)
            {
                ExceptionThrower.ThrowInvalidValueException(CreateExceptionMessage(errorMessage, Resources.ErrorInvalidType), expectedType);
            }

            return(isValid);
        }
Exemple #4
0
        public static bool CheckIsCondition <T>(this T input, bool condition, bool throwException = false, string errorMessage = "")
        {
            var isValid = input is not null && condition;

            if (isValid is false && throwException)
            {
                ExceptionThrower.ThrowInvalidValueException <object>(CreateExceptionMessage(errorMessage, Resources.ErrorInvalidValue), input !);
            }

            return(isValid);
        }
Exemple #5
0
        public static Type ArgumentEquals(this Type input, Type expectedType, string errorMessage = "", [CallerArgumentExpression("input")] string paramName = "")
        {
            input        = input.ArgumentNotNull();
            expectedType = expectedType.ArgumentNotNull();

            if (input.CheckEquals(expectedType) is false)
            {
                ExceptionThrower.ThrowArgumentInvalidException(CreateExceptionMessage(errorMessage, Resources.ErrorInvalidType), paramName);
            }

            return(input !);
        }
Exemple #6
0
        public static DirectoryInfo ArgumentExists(this DirectoryInfo input, DirectoryInfo?defaultValue = null, string errorMessage = "", [CallerArgumentExpression("input")] string paramName = "")
        {
            if (input.CheckIsNotNull() is false)
            {
                ExceptionThrower.ThrowArgumentNullException(CreateParamExceptionMessage(errorMessage, paramName, Resources.ErrorArgumentNull));
            }

            var isValid = input !.Exists;

            if (isValid is false && defaultValue is not null)
            {
                input = defaultValue !;
            }
Exemple #7
0
        public static T FromJsonFile <T>(string fileName)
            where T : class
        {
            fileName = fileName.ArgumentNotNullOrEmpty(true);

            if (File.Exists(fileName) is false)
            {
                ExceptionThrower.ThrowFileNotFoundException(Resources.ErrorFileNotFound, fileName);
            }

            var json = File.ReadAllText(fileName, Encoding.UTF8);

            return(JsonSerializer.Deserialize <T>(json));
        }