protected override object OnDeserializing(JToken jToken)
        {
            // allow base class to notify event handlers (if any) to pre-process the JSON value.
            var value      = base.OnDeserializing(jToken);
            var collection = value as ICollection;

            if (value == null)
            {
                if (jToken.Type == JTokenType.Array)
                {
                    OptionValueParser.ThrowInvalidValueIf(jToken.Any(i => i.Type != JTokenType.String), jToken, this);
                    collection = jToken.Select(i => i.Value <string>()).ToList();
                }
                else if (jToken.Type == JTokenType.String)
                {
                    // allow for a single-value list to be represented with a string.
                    collection = new List <string>()
                    {
                        jToken.Value <string>()
                    };
                }
            }

            OptionValueParser.ThrowInvalidValueIf(collection == null, jToken, this);

            foreach (var item in collection)
            {
                this.InnerList.Add(OptionValueParser.ParseValue <TValue>(item, this));
            }

            // ensure the underlying value is the inner list.
            return(this.InnerList);
        }
 protected override object OnValueChanging(object value)
 {
     // allow any event handler to pre-process the value.
     value = base.OnValueChanging(value);
     if (value != null)
     {
         // ensure the value is of the right type.
         value = OptionValueParser.ParseValue <TValue>(value, this);
     }
     return(value);
 }
        protected override object OnValueChanging(object value)
        {
            // allow base class to notify event handlers (if any) to pre-process the value.
            value = base.OnValueChanging(value);

            // a null value means, the value is being removed.
            if (value != null)
            {
                // ensure the value is of the right type.
                value = OptionValueParser.ParseValue <TValue>(value, this);
            }
            return(value);
        }
        protected override object OnDeserializing(JToken jToken)
        {
            // allow base class to notify event handlers (if any) to pre-process the JSON value.
            var value = base.OnDeserializing(jToken);

            if (value == null)
            {
                value = jToken.Value <string>();
            }

            // ensure the value is of the right type.
            value = OptionValueParser.ParseValue <TValue>(value, this);
            return(value);
        }
 /// <summary>
 /// Core method for setting the option and/or adding values to the collection.
 /// Setting the value has the same semantics as when the option is specified in the command line, when specified multiple times
 /// it is equivalent to having a collection of values not a collection of options and the operation is Add instead of SetValue.
 /// </summary>
 protected override void SetValue(object value)
 {
     if (value == null)
     {
         this.InnerList.Clear();
     }
     else
     {
         if (value is IList <TValue> list)
         {
             this.InnerList.AddRange(list);
         }
         else
         {
             this.InnerList.Add(OptionValueParser.ParseValue <TValue>(value, this));
         }
     }
 }
Exemple #6
0
 int IList.Add(object value)
 {
     this.Add(OptionValueParser.ParseValue <TValue>(value, this.Owner));
     return(this.Count - 1);
 }