SelectedListViewItemCollection如何实现IList但没有Add()? [英] How does SelectedListViewItemCollection implement IList but not have Add()?

查看:206
本文介绍了SelectedListViewItemCollection如何实现IList但没有Add()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图模仿ListView和其他控件处理SelectedItems集合的方式。我有一个包含项集合的类,每个Item都有一个Selected属性。

I am trying to mimic the way a ListView and other controls handle SelectedItems collections. I have a class with a collection of items and each Item has a Selected property.

我想模仿Item可以更改自己的Selected属性的智能行为这样做会在父类中引发SelectedItemsChanged事件,而且SelectedItems集合现在应该反映更改。

I want to mimic the smart behavior where an Item can change its own Selected property and upon doing so would raise a SelectedItemsChanged event in the parent class, and the SelectedItems collection should now reflect the change.

我正在尝试实现一个不包含的SelectedItemsCollection类内部列表,但改为检查主项目列表中每个项目的选定状态。这样就不需要不断更新并与主列表同步。

I am trying to implement a SelectedItemsCollection class which does not contain an inner list but instead checks the Selected state of each item in the main Items list. This way it doesn't need to be constantly updated and synchronized with the main list.

我正在查看ListView.SelectedListViewItemCollection的元数据,它具有以下声明:

I was looking at the metadata for ListView.SelectedListViewItemCollection and it has the following declaration:

public class SelectedListViewItemCollection : IList, ICollection, IEnumerable

它没有实现Add,Remove,RemoveAt等。这是不是违反了使用IList接口的规则?没有实现它我无法编译。这只是元数据创建方式的一个小问题吗?

It does not implement Add, Remove, RemoveAt, etc. Isn't this against the rules of using the IList interface? I cannot compile without implementing them. Is this just a glitch in the way the metadata is created?

我应该如何模仿这个功能?

How should I go about emulating this functionality?

推荐答案

这些方法已实现 明确

例如,使用ILspy反汇编类,您可以看到 Add()方法实现如下:

Those methods are implemented explicitly.
For example disassembling the class with ILspy you can see that the Add() method is implemented as follows:

int IList.Add(object value)
{
    throw new NotSupportedException();
}

因此,例如,如果你有一个的实例SelectedItemsCollection 并且要调用 Add()方法,必须将对象强制转换为 IList 例如:

So, for instance if you have an instance of SelectedItemsCollection and you want to invoke the Add() method, you must cast the object to IList e.g. :

((IList)mySelectedItemsCollection).Add(...)

是的,如果你通过Visual Studio查看类的元数据,隐藏显式实现的方法。

And yes, if you look at the metadata of the class through Visual Studio, the explicitly implemented methods are hidden.

关于已选择属性...... 技巧是每个项目都引用它属于ListView 。当 Selected 属性发生更改时,该项调用 setSelectedState()提出的listView方法SelectedChanged event。

About the Selected property... well the "trick" is that each item has a reference to the ListView it belongs to. When Selected property changes, the item calls SetSelectedState() method of the listView that raises a SelectedChanged event.

同时 SelectedItemsCollection 是通过创建的ListView 在构造函数中引用,这样元素的枚举实际上是在ListView的内部成员上执行的,可以从 SelectedItemsCollection 访问,因为它是一个嵌套的类。

Also SelectedItemsCollection is created passing the ListView reference in the constructor, in this way the enumerations of the elements is actually performed on an inner member of ListView that is accessible from SelectedItemsCollection because it's a nested class.

这篇关于SelectedListViewItemCollection如何实现IList但没有Add()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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