コード例 #1
0
        /// <summary>
        /// Returns a list of parameters for a given options class. If a key prefix is provided, the
        /// keys for the new parameters will be nested under the key prefix. E.g. if the key prefix
        /// `foo` is passed and the options class contains a parameter `bar`, then a parameter
        /// with key `foo[bar]` will be returned.
        /// </summary>
        /// <param name="options">The options class for which to create the list of parameters.</param>
        /// <param name="keyPrefix">The key under which new keys should be nested, if any.</param>
        /// <returns>The list of parameters</returns>
        private static List <Parameter> FlattenParamsOptions(INestedOptions options, string keyPrefix)
        {
            List <Parameter> flatParams = new List <Parameter>();

            if (options == null)
            {
                return(flatParams);
            }

            foreach (var property in options.GetType().GetRuntimeProperties())
            {
                var value = property.GetValue(options, null);

                // Fields on a class which are never set by the user will contain null values (for
                // reference types), so skip those to avoid encoding them in the request.
                if (value == null)
                {
                    continue;
                }

                var attribute = property.GetCustomAttribute <JsonPropertyAttribute>();
                if (attribute == null)
                {
                    continue;
                }

                string key       = attribute.PropertyName;
                string newPrefix = NewPrefix(key, keyPrefix);

                flatParams.AddRange(FlattenParamsValue(value, newPrefix));
            }

            return(flatParams);
        }
コード例 #2
0
        /// <summary>
        /// Returns a list of parameters for a given options class. If a key prefix is provided, the
        /// keys for the new parameters will be nested under the key prefix. E.g. if the key prefix
        /// `foo` is passed and the options class contains a parameter `bar`, then a parameter
        /// with key `foo[bar]` will be returned.
        /// </summary>
        /// <param name="options">The options class for which to create the list of parameters.</param>
        /// <param name="keyPrefix">The key under which new keys should be nested, if any.</param>
        /// <returns>The list of parameters</returns>
        private static List <KeyValuePair <string, object> > FlattenParamsOptions(
            INestedOptions options,
            string keyPrefix)
        {
            List <KeyValuePair <string, object> > flatParams = new List <KeyValuePair <string, object> >();

            if (options == null)
            {
                return(flatParams);
            }

            foreach (var property in options.GetType().GetRuntimeProperties())
            {
                // `[JsonExtensionData]` tells the serializer to write the values contained in
                // the collection as if they were class properties.
                var extensionAttribute = property.GetCustomAttribute <JsonExtensionDataAttribute>();
                if (extensionAttribute != null)
                {
                    var extensionValue = property.GetValue(options, null) as IDictionary;

                    flatParams.AddRange(FlattenParamsDictionary(extensionValue, keyPrefix));
                    continue;
                }

                // Skip properties not annotated with `[JsonProperty]`
                var attribute = property.GetCustomAttribute <JsonPropertyAttribute>();
                if (attribute == null)
                {
                    continue;
                }

                var value = property.GetValue(options, null);

                // Fields on a class which are never set by the user will contain null values (for
                // reference types), so skip those to avoid encoding them in the request.
                if (value == null)
                {
                    continue;
                }

                string key       = attribute.PropertyName;
                string newPrefix = NewPrefix(key, keyPrefix);

                flatParams.AddRange(FlattenParamsValue(value, newPrefix));
            }

            return(flatParams);
        }
コード例 #3
0
        private static List <Parameter> FlattenParamsOptions(INestedOptions options, string keyPrefix)
        {
            List <Parameter> list = new List <Parameter>();

            if (options == null)
            {
                return(list);
            }
            foreach (PropertyInfo runtimeProperty in options.GetType().GetRuntimeProperties())
            {
                object value = runtimeProperty.GetValue(options, null);
                if (value != null)
                {
                    JsonPropertyAttribute customAttribute = runtimeProperty.GetCustomAttribute <JsonPropertyAttribute>();
                    if (customAttribute != null)
                    {
                        string propertyName = customAttribute.PropertyName;
                        string keyPrefix2   = NewPrefix(propertyName, keyPrefix);
                        list.AddRange(FlattenParamsValue(value, keyPrefix2));
                    }
                }
            }
            return(list);
        }