/// <summary>
 /// Returns the value as a dictionary if the current value represents an object.
 /// Otherwise returns null.
 /// </summary>
 public IDictionary <string, DynamicValue> ToDictionary()
 {
     if (!(_value is IDictionary <string, object> dict))
     {
         return(null);
     }
     return(DynamicDictionary.Create(dict));
 }
        public T Get <T>(string path)
        {
            var dynamicDictionary = Value switch
            {
                DynamicDictionary v => v,
                IDictionary <string, object> v => DynamicDictionary.Create(v),
                _ => null,
            };

            return(dynamicDictionary == null ? default : dynamicDictionary.Get <T>(path));
        }
        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);
        }