[英]C# Class with recursion
I am trying to create a class that contains a list of "items" within it. 我正在尝试创建一个其中包含“项目”列表的类。 Which I have successfully done, however then I would like to create a list of items within the list of items.
我已成功完成此操作,但是随后我想在项目列表中创建项目列表。 I have also been able to do this however I had to use a different name for the class within the item.
我也能够做到这一点,但是我必须为该项目中的类使用其他名称。
I would like to use the same class name as this will be used to generate some json where the class name is important. 我想使用相同的类名,因为它将用于生成一些其中类名很重要的json。 In addition I would like to be able to do this in a way where it could be recursive like a folder structure.
另外,我希望能够以一种像文件夹结构一样递归的方式来执行此操作。 All the properties would be the same for each.
每个属性的所有属性都相同。 I hope I am explaining this well enough.
我希望我已经解释得足够好了。 I am essentially trying to create a folder / file structure where there can be x number of files in each folder that can also have x number of folders and so forth.
我实质上是在尝试创建一个文件夹/文件结构,其中每个文件夹中可以有x个文件,也可以有x个文件夹,依此类推。
For example: 例如:
DocLib DocLib
-Item
-项目
--Item.Items
--Item.Items
---Item.Items.Items
--- Item.Items.Items
--Item.Items
--Item.Items
-Item 2 etc...
-项目2等...
Here is the existing code: 这是现有的代码:
public class DocLib
{
public string Title { get; set; }
public string spriteCssClass { get { return "rootfolder"; } }
public List<item> items { get; set; }
public DocLib()
{
items = new List<item>();
}
public class item
{
public string Title { get; set; }
public string spriteCssClass { get; set; }
public List<document> documents { get; set; }
public item()
{
documents = new List<document>();
}
public class document
{
public string Title { get; set; }
public string spriteCssClass { get; set; }
}
}
}
I am sure there is probably a better way of implementing this. 我相信可能有更好的方法来实现这一点。
Just let items be a list of your "own" type 只是让项目成为您自己的类型的列表
public class DocLib{
public string Title { get; set; }
public string spriteCssClass { get { return "rootfolder"; } }
List<DocLib> _items;
public DocLib(){
_items = new List<DocLib>();
}
public List<DocLib> Items {
get{
return _items;
}
}
}
EDIT usage sample: 编辑用法示例:
public static class DocLibExtensions {
public static void Traverse(this DocLib lib,Action<DocLib> process) {
foreach (var item in lib.Items) {
process(item);
item.Traverse(process);
}
}
}
class Program {
static void Main(string[] args) {
DocLib rootDoc = new DocLib {Title = "root"};
rootDoc.Items.Add( new DocLib{ Title = "c1" });
rootDoc.Items.Add(new DocLib { Title = "c2" });
DocLib child = new DocLib {Title = "c3"};
child.Items.Add(new DocLib {Title = "c3.1"});
rootDoc.Items.Add(child);
rootDoc.Traverse(i => Console.WriteLine(i.Title));
}
}
You could also do this with generics. 您也可以使用泛型执行此操作。
public class DocLib<T>
{
public T Item { get; set; }
public IEnumerable<DocLib<T>> Items { get; set; }
}
public class Item
{
public string Title { get; set; }
public string spriteCssClass { get; set; }
}
//Usage
//Set up a root item, and some sub-items
var lib = new DocLib<Item>();
lib.Item = new Item { Title="ABC", spriteCssClass="DEF" };
lib.Items = new List<DocLib<Item>> {
new DocLib<Item>{ Item = new Item {Title="ABC2", spriteCssClass="DEF2"} },
new DocLib<Item>{ Item = new Item {Title="ABC3", spriteCssClass="DEF3"} }
};
//Get the values
var root = lib.Item;
var subItems = lib.Items.Select(i=>i.Item);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.