コード例 #1
0
        /// <summary>
        /// Tries to get the value of the specified object as the specified type.
        /// </summary>
        /// <typeparam name="T">A T that is the type that the object will be cast as.</typeparam>
        /// <param name="Value">An object that is the object to be cast.</param>
        /// <param name="value">A T that is the cast object, or the default value of T.</param>
        /// <returns>A bool indicating success.</returns>
        public static bool GetObjectAsValue <T>(object Value, out T value)
        {
            bool returnValue = false;

            // Defaults
            value = default;

            if (Value != null)
            {
                if (Value is T)
                {
                    value       = (T)Value;
                    returnValue = true;
                }
                else if (typeof(T).IsEnum)
                {
                    value       = (T)Enum.Parse(typeof(T), Value.ToString());
                    returnValue = true;
                }
                else
                {
                    MethodInfo TryParseMethod;

                    TryParseMethod = typeof(T).GetMethod("TryParse", new Type[] { typeof(string), typeof(T).MakeByRefType() });
                    if (TryParseMethod != null)
                    {
                        object   result;
                        object[] tryParseParams;

                        tryParseParams = new object[] { Value.ToString(), null };
                        result         = TryParseMethod.Invoke(null, tryParseParams);
                        if (result != null && result is bool && (bool)result)
                        {
                            value       = (T)tryParseParams[1];
                            returnValue = true;
                        }
                    }
                }
            }

            return(returnValue);
        }
コード例 #2
0
        /// <summary>
        /// Validates a value for range constraints
        /// </summary>
        /// <typeparam name="T">Type of object to be validated (int, double,...etc)</typeparam>
        /// <param name="e">e.Cancel should be set to true if validation fails</param>
        /// <param name="method">Delegate to try parsing the Text property</param>
        private void ValidateValueRange <T>(CancelEventArgs e, TryParseMethod <T> tryParseMethod) where T : struct, IComparable, IConvertible
        {
            bool parseError = false;
            T    val;

            if (tryParseMethod.Invoke(Text, out val))
            {
                T minimumValue, maximumValue;

                // Minimum
                if (!string.IsNullOrEmpty(MinimumValue))
                {
                    if (tryParseMethod.Invoke(MinimumValue, out minimumValue))
                    {
                        if (MinimumValueIncluded)
                        {
                            e.Cancel = val.CompareTo(minimumValue) < 0;
                        }
                        else
                        {
                            e.Cancel = val.CompareTo(minimumValue) < 0 || val.CompareTo(minimumValue) == 0;
                        }
                        if (e.Cancel)
                        {
                            ErrorProvider.SetError(this, ActualBelowMinimumErrorMessage);
                            return;
                        }
                    }
                    else
                    {
                        parseError = true;
                    }
                }

                // Maximum
                if (!string.IsNullOrEmpty(MaximumValue))
                {
                    if (tryParseMethod.Invoke(MaximumValue, out maximumValue))
                    {
                        if (MaximumValueIncluded)
                        {
                            e.Cancel = val.CompareTo(maximumValue) > 0;
                        }
                        else
                        {
                            e.Cancel = val.CompareTo(maximumValue) > 0 || val.CompareTo(maximumValue) == 0;
                        }
                        if (e.Cancel)
                        {
                            ErrorProvider.SetError(this, ActualAboveMaximumErrorMessage);
                            return;
                        }
                    }
                    else
                    {
                        parseError = true;
                    }
                }
            }
            else
            {
                parseError = true;
            }

            if (parseError)
            {
                e.Cancel = true;
                ErrorProvider.SetError(this, Messages.InvalidInput);
            }
        }