Beispiel #1
0
        private static bool SetSpecialTypes <TResponse>(string mimeType, byte[] bytes, IMemoryStreamFactory memoryStreamFactory, out TResponse cs)
            where TResponse : class, IOpenSearchResponse, new()
        {
            cs = null;
            var responseType = typeof(TResponse);

            if (!SpecialTypes.Contains(responseType))
            {
                return(false);
            }

            if (responseType == typeof(StringResponse))
            {
                cs = new StringResponse(bytes.Utf8String()) as TResponse;
            }
            else if (responseType == typeof(BytesResponse))
            {
                cs = new BytesResponse(bytes) as TResponse;
            }
            else if (responseType == typeof(VoidResponse))
            {
                cs = new VoidResponse() as TResponse;
            }
            else if (responseType == typeof(DynamicResponse))
            {
                //if not json store the result under "body"
                if (mimeType == null || !mimeType.StartsWith(RequestData.MimeType))
                {
                    var dictionary = new DynamicDictionary();
                    dictionary["body"] = new DynamicValue(bytes.Utf8String());
                    cs = new DynamicResponse(dictionary) as TResponse;
                }
                else
                {
                    using var ms = memoryStreamFactory.Create(bytes);
                    var body = LowLevelRequestResponseSerializer.Instance.Deserialize <DynamicDictionary>(ms);
                    cs = new DynamicResponse(body) as TResponse;
                }
            }
            return(cs != null);
        }
        internal bool TryParse(object defaultValue, Type targetReturnType, object value, out object newObject)
        {
            newObject = defaultValue;
            if (value == null)
            {
                return(false);
            }

            if (targetReturnType.IsGenericType && targetReturnType.GetGenericTypeDefinition() == typeof(Nullable <>))
            {
                targetReturnType = targetReturnType.GenericTypeArguments[0];
            }

            try
            {
                var valueType = value.GetType();
                if (targetReturnType.IsArray && value is DynamicValue v)
                {
                    value     = v.Value;
                    valueType = value.GetType();
                }
                if (targetReturnType.IsArray)
                {
                    if (!valueType.IsArray)
                    {
                        return(false);
                    }
                    var ar          = (object[])value;
                    var t           = targetReturnType.GetElementType();
                    var objectArray = ar
                                      .Select(a => TryParse(defaultValue, t, a, out var o) ? o : null)
                                      .Where(a => a != null)
                                      .ToArray();

                    var arr = Array.CreateInstance(t, objectArray.Length);
                    Array.Copy(objectArray, arr, objectArray.Length);
                    newObject = arr;
                    return(true);
                }

                if (valueType.IsAssignableFrom(targetReturnType))
                {
                    newObject = value;
                    return(true);
                }

                var stringValue = value as string;

                if (targetReturnType == typeof(DateTime) &&
                    DateTime.TryParse(stringValue, CultureInfo.InvariantCulture, DateTimeStyles.None, out var result))
                {
                    newObject = result;
                    return(true);
                }
                if (stringValue != null)
                {
                    if (targetReturnType == typeof(object))
                    {
                        newObject = Convert.ChangeType(value, targetReturnType);
                        return(true);
                    }

                    var converter = TypeDescriptor.GetConverter(targetReturnType);
                    if (converter.IsValid(stringValue))
                    {
                        newObject = converter.ConvertFromInvariantString(stringValue);
                        return(true);
                    }
                }
                else if (value is DynamicValue dv)
                {
                    return(dv.TryParse(defaultValue, targetReturnType, dv.Value, out newObject));
                }
                else if (targetReturnType == typeof(string))
                {
                    newObject = Convert.ChangeType(value, TypeCode.String, CultureInfo.InvariantCulture);
                    return(true);
                }
                else if (valueType.IsValueType)
                {
                    newObject = Convert.ChangeType(_value, targetReturnType);
                    return(true);
                }
                else if (targetReturnType == typeof(DynamicDictionary) && valueType == typeof(Dictionary <string, object>))
                {
                    newObject = DynamicDictionary.Create(value as Dictionary <string, object>);
                    return(true);
                }

                else if (targetReturnType == typeof(object))
                {
                    newObject = value;
                    return(true);
                }
            }
            catch
            {
                return(false);
            }
            return(false);
        }