Beispiel #1
0
 public static bool TryParseSelectableValueFromString <[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] TValue>(
     this InputBase <TValue> input, string?value,
     [MaybeNullWhen(false)] out TValue result,
     [NotNullWhen(false)] out string?validationErrorMessage)
 {
     try
     {
         if (BindConverter.TryConvertTo <TValue>(value, CultureInfo.CurrentCulture, out var parsedValue))
         {
             result = parsedValue;
             validationErrorMessage = null;
             return(true);
         }
         else
         {
             result = default;
             validationErrorMessage = $"The {input.DisplayName ?? input.FieldIdentifier.FieldName} field is not valid.";
             return(false);
         }
     }
     catch (InvalidOperationException ex)
     {
         throw new InvalidOperationException($"{input.GetType()} does not support the type '{typeof(TValue)}'.", ex);
     }
 }
Beispiel #2
0
        public static bool TryParseSelectableValueFromString <[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] TValue>(
            this InputBase <TValue> input, string?value,
            [MaybeNullWhen(false)] out TValue result,
            [NotNullWhen(false)] out string?validationErrorMessage)
        {
            try
            {
                // We special-case bool values because BindConverter reserves bool conversion for conditional attributes.
                if (typeof(TValue) == typeof(bool))
                {
                    if (TryConvertToBool(value, out result))
                    {
                        validationErrorMessage = null;
                        return(true);
                    }
                }
                else if (typeof(TValue) == typeof(bool?))
                {
                    if (TryConvertToNullableBool(value, out result))
                    {
                        validationErrorMessage = null;
                        return(true);
                    }
                }
                else if (BindConverter.TryConvertTo <TValue>(value, CultureInfo.CurrentCulture, out var parsedValue))
                {
                    result = parsedValue;
                    validationErrorMessage = null;
                    return(true);
                }

                result = default;
                validationErrorMessage = $"The {input.DisplayName ?? input.FieldIdentifier.FieldName} field is not valid.";
                return(false);
            }
            catch (InvalidOperationException ex)
            {
                throw new InvalidOperationException($"{input.GetType()} does not support the type '{typeof(TValue)}'.", ex);
            }
        }