整理多个未知对象的集合
我有一个场景,我有一个根类型,项目中的很多很多对象实现为一个集合,就像这样......
class Page {
// ...
}
interface IPages{
IList<Page> Pages { get; set; }
}
然后有几十个对象,几乎项目中的所有内容都有 的集合页面
。
我需要做的是从根对象开始沿着结构树一路获取所有 Page
对象。然而,这可能非常抽象,并且很难确切地知道将会存在什么。一个简单的“foreach”循环似乎不适合它...
有没有什么方法可以从一个对象开始,检查其中的所有内容以查看它是否具有接口,如果有,则将适当的数据拉出到一个对象中主集合,递归?
I have a scenario where I have a root type that many, many, many objects in my project implement as a collection, like so...
class Page {
// ...
}
interface IPages{
IList<Page> Pages { get; set; }
}
And then there are dozens of objects, almost everything in the project has a collection of Page
.
What I need to do is to get all of the Page
objects all the way down the structure tree from a root object. However this can be very abstract, and knowing exactly what will exist is difficult. A simple 'foreach' loop doesn't seem appropriate for it...
Is there any way to start from one object, and examine everything in it to see if it has an interface, and if so, pull the appropriate data out into a master collection, recursively?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
很难说,因为 Page 似乎没有在您的示例中实现 IPages。
根据您的实现,此扩展方法可能会起作用......
Its hard to tell, since Page doesn't appear to implement IPages in your example.
Depending on your implementation, this extension method might work...