Esempio n. 1
0
        public static T IsOrIsDerivedFrom <T>(object parameter, string parameterName)
        {
            ValidateArgument.IsNotNull(parameterName, nameof(parameterName));
            ValidateArgument.IsNotNull(parameter, parameterName);

            if (!(parameter is T))
            {
                var message = $"Type of argument is not equal to or derived from type {typeof(T)}: {parameter.GetType()}";
                throw new ArgumentException(message, parameterName);
            }

            return((T)parameter);
        }
Esempio n. 2
0
        public static T IsDerivedFrom <T>(object parameter, string parameterName)
            where T : class
        {
            ValidateArgument.IsNotNull(parameterName, nameof(parameterName));
            ValidateArgument.IsNotNull(parameter, parameterName);

            if (typeof(T) == parameter.GetType() || !(parameter is T))
            {
                var message = $"Type of argument is not derived from type ${typeof(T)}: {parameter.GetType()}";
                throw new ArgumentException(message, parameterName);
            }

            return((T)parameter);
        }
Esempio n. 3
0
        /// <summary>
        /// Throws <see cref="ArgumentNullException"/> if the specified argument is null, or <see cref="ArgumentException"/>
        /// if the specified argument empty, or contains only whitespace.
        /// </summary>
        /// <param name="parameter">The argument.</param>
        /// <param name="parameterName">The argument name.</param>
        /// <param name="message">A message that describes the error. Optional.</param>
        /// <exception cref="ArgumentNullException">Either <paramref name="parameterName"/> or <paramref name="parameter"/> is <c>null</c>.</exception>
        /// <exception cref="ArgumentException">The <paramref name="parameter"/> is empty or contains only whitespace.</exception>
        /// <returns><paramref name="parameter"/></returns>
        public static string IsNotNullOrWhiteSpace(string parameter, string parameterName, string message = null)
        {
            ValidateArgument.IsNotNull(parameterName, nameof(parameterName));

            if (parameter == null)
            {
                throw new ArgumentNullException(parameterName, message);
            }
            else if (string.IsNullOrWhiteSpace(parameter))
            {
                var exceptionMessage = message ?? "Argument cannot be null, empty, or contain only whitespace.";
                throw new ArgumentException(parameterName, message);
            }

            return(parameter);
        }
Esempio n. 4
0
        public static T ImplementsInterface <T>(object parameter, string parameterName)
            where T : class
        {
            ValidateArgument.IsNotNull(parameterName, nameof(parameterName));
            ValidateArgument.IsNotNull(parameter, parameterName);

            if (!typeof(T).GetTypeInfo().IsInterface)
            {
                var message = string.Format("Generic argument type is not an interface: {0}", typeof(T));
                throw new ArgumentException(message, nameof(T));
            }

            var result = parameter as T;

            if (result == null)
            {
                var message = string.Format("Type of argument does not implement interface type {0}: {1}", typeof(T), parameter.GetType());
                throw new ArgumentException(message, parameterName);
            }

            return(result);
        }
Esempio n. 5
0
 public DisposeActionScope(Action disposeAction)
 {
     ValidateArgument.IsNotNull(disposeAction, nameof(disposeAction));
     _disposeAction = disposeAction;
 }
Esempio n. 6
0
 public DisposeActionScope(Action initializeAction, Action disposeAction)
     : this(disposeAction)
 {
     ValidateArgument.IsNotNull(initializeAction, nameof(initializeAction));
     initializeAction();
 }