如何将SelectedListViewItemCollection转换为ListViewItemCollection

Ste*_*ows 1 c# listview winforms

我正在尝试编写一个简单的例程来处理ListView中的项目列表,并且只能处理所有项目或所选项目.我希望这可行:

private void PurgeListOfStudies(ListView.ListViewItemCollection lvic)
{
    /// process items in the list...
}
Run Code Online (Sandbox Code Playgroud)

然后像这样调用它:

PurgeListOfStudies(myStudiesPageCurrent.ListView.Items);
Run Code Online (Sandbox Code Playgroud)

或这个

PurgeListOfStudies(myStudiesPageCurrent.ListView.SelectedItems);
Run Code Online (Sandbox Code Playgroud)

然而,这两个名单有不同的和无关的类型,ListViewItemCollectionSelectedListViewItemCollection分别.

我已经尝试将参数的类型更改为object,ICollection<ListViewItem>以及其他一些内容.但由于类型似乎完全不相关,所以在编译时或在演员期间的运行时都会失败.

这一切对我来说都很奇怪,因为这些显然是现实中的相同类型(ListViewItems 列表).

我在这里错过了什么吗?

BTo*_*TKD 5

使用MSDN文档.

如您所见,这两个类都实现了接口:IList,ICollectionIEnumerable.您应该可以将其中任何一个用作通用接口.

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

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