将 ServiceStack 自动查询与未实现 IConvertible 的值类型一起使用

Using ServiceStack Autoquery With Value Types that don't implement IConvertible

我试图在查询参数上使用 AutoQueryNodaTime.LocalDate,当我尝试使用该日期字段进行过滤时,特别是 >MyDate=2020-01-01(排序不受影响):

[MyEndpoint: 5/23/2016 4:19:51 PM]: [REQUEST: {}] System.InvalidCastException: Invalid cast from 'System.String' to 'NodaTime.LocalDate'. at System.Convert.DefaultToType(IConvertible value, Type targetType, IFormatProvider provider) at ServiceStack.TypedQuery`2.AppendUntypedQueries(SqlExpression`1 q, Dictionary`2 dynamicParams, String defaultTerm, IAutoQueryOptions options, Dictionary`2 aliases) at ServiceStack.TypedQuery`2.CreateQuery(IDbConnection db, IQueryDb dto, Dictionary`2 dynamicParams, IAutoQueryOptions options) at ServiceStack.AutoQuery.CreateQuery[From](IQueryDb`1 dto, Dictionary`2 dynamicParams, IRequest req) at ServiceStack.AutoQueryServiceBase.Exec[From](IQueryDb`1 dto) at ServiceStack.Host.ServiceRunner`1.Execute(IRequest request, Object instance, TRequest requestDto)

我追踪到使用 Convert.ChangeType(...)this line of code 因为 NodaTime.LocalDatestruct 而不是 enum:

var value = strValue == null ? 
      null 
    : isMultiple ? 
      TypeSerializer.DeserializeFromString(strValue, Array.CreateInstance(fieldType, 0).GetType())
    : fieldType == typeof(string) ? 
      strValue
    : fieldType.IsValueType && !fieldType.IsEnum ? //This is true for NodaTime.LocalDate
      Convert.ChangeType(strValue, fieldType) :    //NodaTime.LocalDate does not implement IConvertible, so this throws
      TypeSerializer.DeserializeFromString(strValue, fieldType);

我正在使用我的 NodaTime ServiceStack 序列化库,因此 TypeSerializer.DeserializeFromString(strValue, fieldType) 的行为正是我在这种情况下真正想要的。

我看到的解决方法是:

是否有另一种方法可以使自动查询过滤器与未实现 IConvertible 的值类型一起使用?

我刚刚添加了将这些行包装在一个新的 ChangeTo() 扩展方法中,并进行了额外检查以检查是否实现了 IConvertible in this commit:

public static object ChangeTo(this string strValue, Type type)
{
    if (type.IsValueType && !type.IsEnum
        && type.HasInterface(typeof(IConvertible)))
    {
        try
        {
            return Convert.ChangeType(strValue, type);
        }
        catch (Exception ex)
        {
            Tracer.Instance.WriteError(ex);
        }
    }
    return TypeSerializer.DeserializeFromString(strValue, type);
}

并且 changed AutoQuery to use it 所以 NodaTime 的 LocalDate 现在应该落入 TypeSerializer。

此更改适用于现在 available on MyGet 的 v4.0.57。