internal static async Task BindAsyncCollectorAsync()

in src/WebJobs.Script/Binding/FunctionBinding.cs [144:202]


        internal static async Task BindAsyncCollectorAsync<T>(BindingContext context)
        {
            IAsyncCollector<T> collector = await context.Binder.BindAsync<IAsyncCollector<T>>(context.Attributes);

            IEnumerable values = ReadAsEnumerable(context.Value);

            // convert values as necessary and add to the collector
            foreach (var value in values)
            {
                object converted = null;
                if (typeof(T) == typeof(string))
                {
                    if (value is ExpandoObject)
                    {
                        converted = Utility.ToJson((ExpandoObject)value, Formatting.None);
                    }
                    else
                    {
                        converted = value.ToString();
                    }
                }
                else if (typeof(T) == typeof(JObject))
                {
                    if (value is JObject)
                    {
                        converted = (JObject)value;
                    }
                    else if (value is ExpandoObject)
                    {
                        converted = Utility.ToJObject((ExpandoObject)value);
                    }
                }
                else if (typeof(T) == typeof(byte[]))
                {
                    byte[] bytes = value as byte[];
                    if (bytes == null)
                    {
                        string stringValue = null;
                        if (value is ExpandoObject)
                        {
                            stringValue = Utility.ToJson((ExpandoObject)value, Formatting.None);
                        }
                        else
                        {
                            stringValue = value.ToString();
                        }

                        bytes = Encoding.UTF8.GetBytes(stringValue);
                    }
                    converted = bytes;
                }
                else
                {
                    throw new ArgumentException("Unsupported collection type.");
                }

                await collector.AddAsync((T)converted);
            }
        }