C# 如何将SelectedListViewItemCollection转换为ListViewItemCollection

C# 如何将SelectedListViewItemCollection转换为ListViewItemCollection,c#,winforms,listview,C#,Winforms,Listview,我正在尝试编写一个简单的例程来处理ListView中的项目列表,并能够处理所有项目或仅处理选定的项目。我希望这能奏效: private void PurgeListOfStudies(ListView.ListViewItemCollection lvic) { /// process items in the list... } 然后这样称呼它: PurgeListOfStudies(myStudiesPageCurrent.ListView.Items); PurgeListOf

我正在尝试编写一个简单的例程来处理ListView中的项目列表,并能够处理所有项目或仅处理选定的项目。我希望这能奏效:

private void PurgeListOfStudies(ListView.ListViewItemCollection lvic)
{
    /// process items in the list...
}
然后这样称呼它:

PurgeListOfStudies(myStudiesPageCurrent.ListView.Items);
PurgeListOfStudies(myStudiesPageCurrent.ListView.Items.OfType<ListViewItem>());
还是这个

PurgeListOfStudies(myStudiesPageCurrent.ListView.SelectedItems);
但是,这两个列表具有不同且不相关的类型,
ListViewItemCollection
SelectedListViewItemCollection

我已尝试将参数的类型更改为
对象
ICollection
以及其他一些内容。但是,由于类型似乎完全不相关,所以在转换过程中,无论是在编译时还是在运行时,所有操作都会失败

这一切对我来说似乎都很奇怪,因为它们在现实中显然是相同的类型(一个
ListViewItem
s的列表)


我在这里遗漏了什么吗?

请使用MSDN文档

如您所见,这两个类都实现了接口:、和。您应该能够将其中任何一个用作公共接口

注:这些不是通用版本(即IEnumerable)。您必须枚举集合并手动将它们转换为所需的对象类型

private void PurgeListOfStudies(IEnumerable items)
{
    foreach(MyType currentItem in items) //implicit casting to desired type
    {
        // process current item in the list...
    }
}

使用MSDN文档

如您所见,这两个类都实现了接口:、和。您应该能够将其中任何一个用作公共接口

注:这些不是通用版本(即IEnumerable)。您必须枚举集合并手动将它们转换为所需的对象类型

private void PurgeListOfStudies(IEnumerable items)
{
    foreach(MyType currentItem in items) //implicit casting to desired type
    {
        // process current item in the list...
    }
}

如果您想使PurgeListOfStudies的类型更加安全,可以让它采用
IEnumerable
类型的参数,如下所示:

private void PurgeListOfStudies(IEnumerable<ListViewItem> items)
{
    /// process items in the list...
}
然后打电话

PurgeListOfStudies(myStudiesPageCurrent.ListView.ListViewItems());


如果您想使PurgeListOfStudies的类型更加安全,可以让它采用
IEnumerable
类型的参数,如下所示:

private void PurgeListOfStudies(IEnumerable<ListViewItem> items)
{
    /// process items in the list...
}
然后打电话

PurgeListOfStudies(myStudiesPageCurrent.ListView.ListViewItems());


我确实读过MSDN,但忽略了非通用的集合。我尝试了几种通用集合的变体。谢谢。我非常感谢你的回答。我一直试图转换为泛型类型,所以现在我知道了我的错误。我确实读过MSDN,但忽略了非泛型集合。我尝试了几种通用集合的变体。谢谢。我非常感谢你的回答。我一直试图转换为泛型类型,所以现在我知道了我的错误。