VB.NET泛型列表类型的过滤器:如何做到这一点? [英] VB.NET Generic list type filter: How to do it?

查看:303
本文介绍了VB.NET泛型列表类型的过滤器:如何做到这一点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想编写一个通用的功能,将搜索列表(Of T)中的类型TFilter的所有元素,并返回一个列表(中TFilter),包括这些元素。我试过这样的:

I would like to write a generic function that would search a List(Of T) for all elements of type TFilter and return a List(Of TFilter) which comprises those elements. I've tried this:

Public Function FilterList(Of T, TFilter)(ByVal ListToFilter As List(Of T)) As List(Of TFilter)
    Return ListToFilter.FindAll(Function(z) z.GetType.Equals(GetType(TFilter))).ConvertAll(New Converter(Of T, TFilter)(Function(z) CType(z, TFilter)))
End Function

但是,它提供了以下错误:

But, it gives the following error:

类型'T'的值不能转换   以TFilter。

Value of type 'T' cannot be converted to 'TFilter'.

有没有办法做到这一点还是我SOL?

Is there any way to do this or am I SOL?

在此先感谢

推荐答案

您需要限制 TFilter 继承 T

Public Function FilterList(Of T, TFilter As T)(...

这篇关于VB.NET泛型列表类型的过滤器:如何做到这一点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆