对 List应用自动和隐藏过滤
好的。
我有一个类 MyClass 和另一个基于 List 的类。我们将其命名为“MyCollection”。
现在,当有人输入:
MyCollection coll = new MyCollection();
...
coll.Find(...)
他们正在对整个集合进行操作。我想在幕后应用一些过滤,这样如果他们编写上面的代码,实际上执行的内容类似于...
coll.Where(x=>x.CanSeeThis).Find(...)
我需要在 MyCollection 类的定义中编写什么让这个工作?
我能完成这个工作吗?
OK.
I have a class MyClass and another class that is based on List. Let's call it MyCollection.
Now when someone types:
MyCollection coll = new MyCollection();
...
coll.Find(...)
They are acting on the entire collection. I want to apply some filtering - behind the scenes - so that if they write the above code, what actually executes is something like...
coll.Where(x=>x.CanSeeThis).Find(...)
What do I need to write in the definition of the MyCollection class to make this work?
Can I make this work?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可能想要编写一个包装类,在内部使用常规的
List
来实现IList
或ICollection
。然后,该包装类会将所有方法调用代理到内部列表,并根据需要应用过滤器。You probably want to write a wrapper class that implements
IList
orICollection
, using a regularList
internally. This wrapper class would then proxy all method calls to the internal list, applying the filter as required.您已经提到您有自己的收藏,可能来自 List,对吧?
然后,您需要创建自己的查找方法:
不幸的是,这是必需的,因为您无法直接重写 List 上的 Find 方法。但是,您可以使用“new”关键字来指定如果您有对 MyList 实例的引用,它将使用 find 的实现,如下所示:
但是上面的示例将产生:
所以最好让您有自己的方法。
You´ve already mentioned you´ve got your own collection, probably derived from List right?
Then you´ll need to create your own method for finding:
This unfortunatly is needed because you cannot override the Find method on List directly. You can however use the 'new' keyword to specify that If you´ve got a reference to the instance of MyList it will use that implementation of find, like below:
However the above example will yield:
So it´s better you make you´re own method.