コード例 #1
0
ファイル: ParameterValidation.cs プロジェクト: ggreig/OXO
        /// <summary>
        /// Ensures the specified value is of the expected type; otherwise throws an ArgumentException.
        /// </summary>
        /// <typeparam name="T">The type of the object being checked.</typeparam>
        /// <param name="inExpectedType">The expected type of the value..</param>
        /// <param name="inValue">The value to check for an expected type.</param>
        /// <param name="inName">The name of the value being checked, as a string.</param>
        /// <returns>The successfully checked value is returned.</returns>
        /// <exception cref="System.ArgumentException">Thrown if the object type is not as expected.</exception>
        public static T EnsureOfType <T>(System.Type inExpectedType, [ValidatedOfType] T inValue, string inName)
        {
            ParameterValidation.EnsureNotNull(inExpectedType, "inExpectedType");

            if (!IsOfType(inExpectedType, inValue))
            {
                string theMessage = string.Format(
                    System.Globalization.CultureInfo.CurrentCulture,
                    "An argument of type {0} was provided (expected {1}).",
                    inValue.GetType().GetGenericAwareFullTypeName(),
                    inExpectedType.GetGenericAwareFullTypeName());
                throw new ArgumentTypeException(theMessage, inName);
            }

            return(inValue);
        }
コード例 #2
0
ファイル: ParameterValidation.cs プロジェクト: ggreig/OXO
        /// <summary>
        /// Determines whether the specified value is of the expected type.
        /// </summary>
        /// <param name="inExpectedType">The expected type of the value..</param>
        /// <param name="inValue">The value to check for an expected type.</param>
        /// <returns>
        /// Value is <see langword="true"/> if the specified value is of the expected type; otherwise <see langword="false"/>.
        /// </returns>
        public static bool IsOfType(System.Type inExpectedType, object inValue)
        {
            ParameterValidation.EnsureNotNull(inValue, "inValue");

            return(inValue.GetType().IsInstanceOfType(inExpectedType));
        }