Beispiel #1
0
        internal static UnvalidatedObjectCollection <NameValueHeaderValue>?Clone(this UnvalidatedObjectCollection <NameValueHeaderValue>?source)
        {
            if (source == null)
            {
                return(null);
            }

            var copy = new UnvalidatedObjectCollection <NameValueHeaderValue>();

            foreach (NameValueHeaderValue item in source)
            {
                copy.Add(new NameValueHeaderValue(item));
            }

            return(copy);
        }
Beispiel #2
0
        internal static void SetQuality(UnvalidatedObjectCollection <NameValueHeaderValue> parameters, double?value)
        {
            Debug.Assert(parameters != null);

            NameValueHeaderValue?qualityParameter = NameValueHeaderValue.Find(parameters, qualityName);

            if (value.HasValue)
            {
                // Note that even if we check the value here, we can't prevent a user from adding an invalid quality
                // value using Parameters.Add(). Even if we would prevent the user from adding an invalid value
                // using Parameters.Add() they could always add invalid values using HttpHeaders.AddWithoutValidation().
                // So this check is really for convenience to show users that they're trying to add an invalid
                // value.
                if ((value < 0) || (value > 1))
                {
                    throw new ArgumentOutOfRangeException(nameof(value));
                }

                string qualityString = ((double)value).ToString("0.0##", NumberFormatInfo.InvariantInfo);
                if (qualityParameter != null)
                {
                    qualityParameter.Value = qualityString;
                }
                else
                {
                    parameters.Add(new NameValueHeaderValue(qualityName, qualityString));
                }
            }
            else
            {
                // Remove quality parameter
                if (qualityParameter != null)
                {
                    parameters.Remove(qualityParameter);
                }
            }
        }