简体   繁体   中英

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. 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.

For example:

DocLib

-Item

--Item.Items

---Item.Items.Items

--Item.Items

-Item 2 etc...

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM