Cast To Interface

前端 未结 2 348
长发绾君心
长发绾君心 2021-01-25 00:08

I am not getting any errors in my code, but my filter object is always null. When I run the debugger the filter object looks just like the sort object, a list with stuff in it.

相关标签:
2条回答
  • 2021-01-25 00:36

    The list is null because you are trying to cast List<IFilterDescriptor> into List<FilterDescriptor>, but just because FilterDescriptor inherits from IFilterDescriptor doesn't mean that List<FilterDescriptor> inherits from List<IFilterDescriptor> (unless the list is covariant List<in T> which it isn't).

    You should do it like this:

    IList<IFilterDescriptor> filter = request.Filters;
    

    if the interface is fine with that, otherwise you can case the list like that:

    List<FilterDescriptor> filter = request.Filters.OfType<FilterDescriptor>.ToList();
    
    0 讨论(0)
  • 2021-01-25 00:50

    Thanks for the help guys I talked to a friend of mine and he suggested I do this... So I guess I should not have been trying to cast the Lists and Interfaces.

        if(request.Sorts.Count == 1)
        {
            pRequest.SortMember = request.Sorts[0].Member;
            pRequest.SortDirection = (int)request.Sorts[0].SortDirection;
        }
    
        if (request.Filters.Count >= 1)
        {
            foreach(var item in request.Filters)
            {
                if(item is Kendo.Mvc.FilterDescriptor)
                {
                     var descriptor = (Kendo.Mvc.FilterDescriptor)item;
                     pRequest.Startdate = (DateTime)descriptor.ConvertedValue;
                }
            }                
        }
        else
        {
             var endDate = new TimeSpan(4000, 0, 0, 0, 0);
             pRequest.Startdate = DateTime.UtcNow - endDate;
        }
    
    0 讨论(0)
提交回复
热议问题