c# 枚举列表以从文件夹结构中映射 parentId

Posted

技术标签:

【中文标题】c# 枚举列表以从文件夹结构中映射 parentId【英文标题】:c# Enumerate List to map parentId's from a folder structure 【发布时间】:2021-10-02 08:12:18 【问题描述】:

我有一个包含此类的列表:

public class RazorTree

    public int id  get; set; 
    public string Name  get; set;         
    public string Path  get; set; 
    public int? parentId  get; set;  = null;

例子:

id = 1
path = "C:\\Projects\\testapp\subdirectoy\\"
name = "root"
parentId = null

id = 45
path = "C:\\Projects\\testapp\subdirectoy\\test.razor"
name = "test"
parentId = null

id = 55
path = "C:\\Projects\\testapp\subdirectoy\\subdirectory2\\test.razor"
name = "test"
parentId = null

我需要的是id = 45 的条目应该有parentId = 1id = 55 应该有parentId = 45,这取决于每个条目的路径。

如何根据每个孩子的路径将孩子与其各自的父母联系起来?

【问题讨论】:

【参考方案1】:

可以供以后参考。

这是我想出的:

public TreeItem GetFileStructure()

    var razorPages = typeof(Qassembly).Assembly.GetTypes().Where(x => x.BaseType?.Name == "LayoutComponentBase" || x.BaseType?.Name == "ComponentBase").ToList();

    var root = new TreeItem  Description = "root" ,Path = "root", ParentId = 0, Level =  4;

    List<string> TreePaths = new();

    foreach (var page in razorPages)
        TreePaths.Add(basePath + page.FullName.Replace(".", "\\") + ".razor");

    foreach (var i in TreePaths)
        root.AddChild(i);

    return root;

        public class TreeItem
        
            static List<TreeItem> FlatList = new List<TreeItem>();
            public TreeItem()
            
                Id = FlatList.Any() ? FlatList.Max(m => m.Id) + 1 : 1;
                FlatList.Add(this);
                Children = new List<TreeItem>();
            
            public string Description  get; set; 
            public string Path  get; set; 
            public int Id  get; set; 
            public int ParentId  get; set; 
            public int Level  get; set; 
            public List<TreeItem> Children  get; set; 

            public void AddChild(string Item)
            
                var items = Item.Split('\\');
                string path = string.Empty;
                for (int i = 0; i <= Level; i++)
                    path += (path == string.Empty ? string.Empty : "\\") + items[i];

                if (path == Path)
                
                    if ((Level + 1) == items.Count())
                        Children.Add(new TreeItem  Description = items[Level], Path = path, Level = Level + 1, ParentId = Id );
                    else
                    
                        path += "\\" + items[Level + 1];
                        var child = Children.FirstOrDefault(a => a.Path == path);
                        if (child == null)
                        
                            child = new TreeItem  Description = path, Path = path, Level = Level + 1, ParentId = Id ;
                            Children.Add(child);
                        
                        child.AddChild(Item);
                    

                
            
        

即使每个孩子还不知道 parentId,这也会从平面列表中映射一棵树。

【讨论】:

以上是关于c# 枚举列表以从文件夹结构中映射 parentId的主要内容,如果未能解决你的问题,请参考以下文章

C# 枚举enum

将某个 JSON 值映射到枚举值 C#

在 Dart 中枚举或映射具有索引和值的列表

创建 C# 代码以从 json 文件中获取值

Atitit. C# java 的api 文件夹封装结构映射总结

如何映射枚举以选择 Storybook 中的下拉列表?