递归的C#类 [英] C# Class with recursion

查看:86
本文介绍了递归的C#类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个其中包含项目"列表的类.我已成功完成此操作,但是随后我想在项目列表中创建项目列表.我也能够做到这一点,但是我必须为该项目中的类使用一个不同的名称.

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.

我想使用相同的类名,因为它将用于生成一些在该类名很重要的json.另外,我希望能够以一种可以像文件夹结构那样递归的方式来执行此操作.每个属性的所有属性都相同.我希望我能对此做足够的解释.我实质上是在尝试创建一个文件夹/文件结构,其中每个文件夹中可以有x个文件,也可以有x个文件夹,依此类推.

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.

例如:

DocLib

-项目

-Item.Items

--Item.Items

-Item.Items.Items

---Item.Items.Items

-Item.Items

--Item.Items

-第2项,等等...

-Item 2 etc...

这是现有代码:

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

编辑用法示例:

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

    }
}

这篇关于递归的C#类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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