ProcessPlugins() static private méthode

static private ProcessPlugins ( string &requestString, JsonPropertyAttribute attribute, PropertyInfo property, object propertyValue, object propertyParent ) : void
requestString string
attribute Newtonsoft.Json.JsonPropertyAttribute
property System.Reflection.PropertyInfo
propertyValue object
propertyParent object
Résultat void
Exemple #1
0
        public bool Parse(ref string requestString, JsonPropertyAttribute attribute, PropertyInfo property, object propertyValue, object propertyParent)
        {
            if (attribute.PropertyName != "subscription_items_array_updated")
            {
                return(false);
            }

            var items = ((List <StripeSubscriptionItemUpdateOption>)property.GetValue(propertyParent, null));

            var itemIndex = 0;

            foreach (var item in items)
            {
                var properties = item.GetType().GetRuntimeProperties();

                foreach (var prop in properties)
                {
                    var value = prop.GetValue(item, null);
                    if (value == null)
                    {
                        continue;
                    }

                    // it must have a json attribute matching stripe's key, and only one
                    var attr = prop.GetCustomAttributes <JsonPropertyAttribute>().SingleOrDefault();
                    if (attr == null)
                    {
                        continue;
                    }
                    // a JsonPropertyAttribute is required to supply a property name to parser plugins.
                    var indexedItemAttribute = new JsonPropertyAttribute($"items[{itemIndex}][{attr.PropertyName}]");
                    RequestStringBuilder.ProcessPlugins(ref requestString, indexedItemAttribute, prop, value, propertyParent);
                }

                itemIndex++;
            }

            return(true);
        }
        public bool Parse(
            ref string requestString,
            JsonPropertyAttribute attribute,
            PropertyInfo property,
            object propertyValue,
            object propertyParent)
        {
            // Check if the property is a List
            var type = property.PropertyType;

            if (!type.GetTypeInfo().IsGenericType)
            {
                return(false);
            }

            if (type.GetTypeInfo().GetGenericTypeDefinition() != typeof(List <>))
            {
                return(false);
            }

            // Cast to List<object>
            var items = ((IEnumerable)propertyValue).Cast <object>().ToList();

            // If the list is empty, just send the parameter's name with an empty string as the
            // value to tell Stripe's API to empty the parameter.
            if (items.Count == 0)
            {
                RequestStringBuilder.ApplyParameterToRequestString(ref requestString, attribute.PropertyName, string.Empty);
                return(true);
            }

            // Encode each item
            var itemIndex = 0;

            foreach (var item in items)
            {
                var itemType = item.GetType();

                if (itemType.GetTypeInfo().IsPrimitive || (itemType == typeof(string)))
                {
                    // Primitive type encoding (string counts as a primitive type)
                    RequestStringBuilder.ApplyParameterToRequestString(
                        ref requestString,
                        $"{attribute.PropertyName}[{itemIndex}]",
                        item.ToString());
                }
                else
                {
                    // Complex type encoding
                    var itemProperties = item.GetType().GetRuntimeProperties();

                    foreach (var itemProp in itemProperties)
                    {
                        var itemAttr = itemProp.GetCustomAttributes <JsonPropertyAttribute>().SingleOrDefault();
                        if (itemAttr == null)
                        {
                            // Skip attributes not tagged with JsonPropertyAttribute
                            continue;
                        }

                        var itemValue = itemProp.GetValue(item, null);
                        if (itemValue == null)
                        {
                            // Ignore null elements
                            continue;
                        }

                        // A JsonPropertyAttribute is required to supply a property name to parser plugins.
                        var key = $"{attribute.PropertyName}[{itemIndex}][{itemAttr.PropertyName}]";
                        var indexedItemAttribute = new JsonPropertyAttribute(key);
                        RequestStringBuilder.ProcessPlugins(ref requestString, indexedItemAttribute, itemProp, itemValue, propertyParent);
                    }
                }

                itemIndex++;
            }

            return(true);
        }