// Convert IEnumerable to array
        public bool TryConvert(ConverterContext context, out object?target)
        {
            target = null;
            // Ensure requested type is an array
            if (context.Parameter.Type.IsArray)
            {
                Type?elementType = context.Parameter.Type.GetElementType();
                if (elementType is not null)
                {
                    // Ensure that we can assign from source to parameter type
                    if (elementType.Equals(typeof(string)) ||
                        elementType.Equals(typeof(byte[])) ||
                        elementType.Equals(typeof(ReadOnlyMemory <byte>)) ||
                        elementType.Equals(typeof(long)) ||
                        elementType.Equals(typeof(double)))
                    {
                        target = context.Source switch
                        {
                            IEnumerable <string> source => source.ToArray(),
                            IEnumerable <ReadOnlyMemory <byte> > source => GetBinaryData(source, elementType !),
                            IEnumerable <double> source => source.ToArray(),
                            IEnumerable <long> source => source.ToArray(),
                            _ => null
                        };
                    }
                }
            }

            return(target is not null);
        }
Esempio n. 2
0
        public bool TryConvert(ConverterContext context, out object?target)
        {
            target = default;

            if (context.Parameter.Type == typeof(string))
            {
                return(false);
            }

            byte[]? bytes = null;

            if (context.Source is string sourceString)
            {
                bytes = Encoding.UTF8.GetBytes(sourceString);
            }
            else if (context.Source is ReadOnlyMemory <byte> sourceMemory)
            {
                bytes = sourceMemory.ToArray();
            }

            if (bytes == null)
            {
                return(false);
            }

            return(TryDeserialize(bytes, context.Parameter.Type, out target));
        }
        public async ValueTask <ConversionResult> ConvertAsync(ConverterContext context)
        {
            if (context.TargetType == typeof(string))
            {
                return(ConversionResult.Unhandled());
            }

            byte[]? bytes = null;

            if (context.Source is string sourceString)
            {
                bytes = Encoding.UTF8.GetBytes(sourceString);
            }
            else if (context.Source is ReadOnlyMemory <byte> sourceMemory)
            {
                bytes = sourceMemory.ToArray();
            }

            if (bytes == null)
            {
                return(ConversionResult.Unhandled());
            }

            return(await GetConversionResultFromDeserialization(bytes, context.TargetType));
        }
        public ValueTask <ConversionResult> ConvertAsync(ConverterContext context)
        {
            // Special handling for the context.
            if (context.TargetType == typeof(FunctionContext))
            {
                return(new ValueTask <ConversionResult>(ConversionResult.Success(context.FunctionContext)));
            }

            return(new ValueTask <ConversionResult>(ConversionResult.Unhandled()));
        }
Esempio n. 5
0
        public ValueTask <ConversionResult> ConvertAsync(ConverterContext context)
        {
            if (context.TargetType == typeof(Guid) || context.TargetType == typeof(Guid?))
            {
                if (context.Source is string sourceString && Guid.TryParse(sourceString, out Guid parsedGuid))
                {
                    return(new ValueTask <ConversionResult>(ConversionResult.Success(parsedGuid)));
                }
            }

            return(new ValueTask <ConversionResult>(ConversionResult.Unhandled()));
        }
Esempio n. 6
0
        public ValueTask <ConversionResult> ConvertAsync(ConverterContext context)
        {
            Type?sourceType = context.Source?.GetType();

            if (sourceType is not null &&
                context.TargetType.IsAssignableFrom(sourceType))
            {
                var conversionResult = ConversionResult.Success(context.Source);
                return(new ValueTask <ConversionResult>(conversionResult));
            }

            return(new ValueTask <ConversionResult>(ConversionResult.Unhandled()));
        }
        public bool TryConvert(ConverterContext context, out object?target)
        {
            target = default;

            if (!(context.Parameter.Type.IsAssignableFrom(typeof(byte[])) &&
                  context.Source is string sourceString))
            {
                return(false);
            }

            target = Encoding.UTF8.GetBytes(sourceString);
            return(true);
        }
        public ValueTask <ConversionResult> ConvertAsync(ConverterContext context)
        {
            if (!(context.TargetType.IsAssignableFrom(typeof(byte[])) &&
                  context.Source is string sourceString))
            {
                return(new ValueTask <ConversionResult>(ConversionResult.Unhandled()));
            }

            var byteArray        = Encoding.UTF8.GetBytes(sourceString);
            var conversionResult = ConversionResult.Success(byteArray);

            return(new ValueTask <ConversionResult>(conversionResult));
        }
        public bool TryConvert(ConverterContext context, out object?target)
        {
            Type?sourceType = context.Source?.GetType();

            if (sourceType is not null &&
                context.Parameter.Type.IsAssignableFrom(sourceType))
            {
                target = context.Source;
                return(true);
            }

            target = default;
            return(false);
        }
        internal bool TryConvert(ConverterContext context, out object?target)
        {
            target = null;

            // The first converter to successfully convert wins.
            // For example, this allows a converter that parses JSON strings to return false if the
            // string is not valid JSON. This manager will then continue with the next matching provider.
            foreach (var converter in _converters)
            {
                if (converter.TryConvert(context, out object?targetObj))
                {
                    target = targetObj;
                    return(true);
                }
            }

            return(false);
        }
        public bool TryConvert(ConverterContext context, out object?target)
        {
            FunctionParameter param = context.Parameter;

            target = null;

            if (param.Type.IsGenericType)
            {
                var genericType = param.Type.GetGenericTypeDefinition();

                if (genericType == typeof(OutputBinding <>))
                {
                    var  elementType = param.Type.GetGenericArguments()[0];
                    Type constructed = defaultBindingType.MakeGenericType(new Type[] { elementType });
                    target = Activator.CreateInstance(constructed, context.Parameter, context.ExecutionContext.OutputBindings);
                    return(true);
                }
            }

            return(false);
        }
        public ValueTask <ConversionResult> ConvertAsync(ConverterContext context)
        {
            if (context.Source is not ReadOnlyMemory <byte> sourceMemory)
            {
                return(new ValueTask <ConversionResult>(ConversionResult.Unhandled()));
            }

            if (context.TargetType.IsAssignableFrom(typeof(string)))
            {
                var target = Encoding.UTF8.GetString(sourceMemory.Span);
                return(new ValueTask <ConversionResult>(ConversionResult.Success(target)));
            }

            if (context.TargetType.IsAssignableFrom(typeof(byte[])))
            {
                var target = sourceMemory.ToArray();
                return(new ValueTask <ConversionResult>(ConversionResult.Success(target)));
            }

            return(new ValueTask <ConversionResult>(ConversionResult.Unhandled()));
        }
Esempio n. 13
0
        public bool TryConvert(ConverterContext context, out object?target)
        {
            target = default;

            if (context.Parameter.Type == typeof(string) ||
                context.Source is not string sourceString ||
                string.IsNullOrEmpty(sourceString))
            {
                return(false);
            }

            try
            {
                target = JsonSerializer.Deserialize(sourceString, context.Parameter.Type, _options.Value);
                return(true);
            }
            catch (JsonException)
            {
            }

            return(false);
        }
        public bool TryConvert(ConverterContext context, out object?target)
        {
            target = default;

            if (context.Source is not ReadOnlyMemory <byte> sourceMemory)
            {
                return(false);
            }

            if (context.Parameter.Type.IsAssignableFrom(typeof(string)))
            {
                target = Encoding.UTF8.GetString(sourceMemory.Span);
                return(true);
            }

            if (context.Parameter.Type.IsAssignableFrom(typeof(byte[])))
            {
                target = sourceMemory.ToArray();
                return(true);
            }

            return(false);
        }
Esempio n. 15
0
        // Convert IEnumerable to array
        public ValueTask <ConversionResult> ConvertAsync(ConverterContext context)
        {
            object?target = null;

            // Ensure requested type is an array
            if (context.TargetType.IsArray)
            {
                Type?elementType = context.TargetType.GetElementType();
                if (elementType is not null)
                {
                    // Ensure that we can assign from source to parameter type
                    if (elementType.Equals(typeof(string)) ||
                        elementType.Equals(typeof(byte[])) ||
                        elementType.Equals(typeof(ReadOnlyMemory <byte>)) ||
                        elementType.Equals(typeof(long)) ||
                        elementType.Equals(typeof(double)))
                    {
                        target = context.Source switch
                        {
                            IEnumerable <string> source => source.ToArray(),
                            IEnumerable <ReadOnlyMemory <byte> > source => GetBinaryData(source, elementType !),
                            IEnumerable <double> source => source.ToArray(),
                            IEnumerable <long> source => source.ToArray(),
                            _ => null
                        };
                    }
                }
            }

            if (target is not null)
            {
                return(new ValueTask <ConversionResult>(ConversionResult.Success(target)));
            }

            return(new ValueTask <ConversionResult>(ConversionResult.Unhandled()));
        }