in src/Elastic.Transport/Responses/Dynamic/DynamicValue.cs [745:811]
public override bool TryConvert(ConvertBinder binder, out object result)
{
result = null;
if (_value == null)
{
return true;
}
var binderType = binder.Type;
if (binderType == typeof(List<DynamicValue>))
{
result = ToEnumerable().ToList();
return true;
}
if (binderType == typeof(List<object>))
{
result = ToEnumerable().Cast<object>().ToList();
return true;
}
if (binderType == typeof(string))
{
result = Convert.ToString(_value);
return true;
}
if (binderType == typeof(Guid) || binderType == typeof(Guid?))
{
if (Guid.TryParse(Convert.ToString(_value), out var guid))
{
result = guid;
return true;
}
}
else if (binderType == typeof(TimeSpan) || binderType == typeof(TimeSpan?))
{
if (TimeSpan.TryParse(Convert.ToString(_value), out var timespan))
{
result = timespan;
return true;
}
}
else
{
if (binderType.IsGenericType && binderType.GetGenericTypeDefinition() == typeof(Nullable<>))
binderType = binderType.GetGenericArguments()[0];
var typeCode = Type.GetTypeCode(binderType);
if (typeCode == TypeCode.Object)
{
if (binderType.IsAssignableFrom(_value.GetType()))
{
result = _value;
return true;
}
else
return false;
}
result = Convert.ChangeType(_value, typeCode);
return true;
}
return base.TryConvert(binder, out result);
}