层级字典构造:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace test1
{
    public class ToDic
    {
        public ToDic(Annotation annotation)
        {
            _annotation = annotation;
        }
        public Annotation _annotation;
        public class Annotation
        {
            public string _folder;
            public string _filename;
            public string _path;
            public Source _source;
            public Size _size;
            public string _segmented;
            //public List<_Object> _objects { get; set; }
            public List<_Object> _objects = new List<_Object>();

            public Annotation(string folder,
                string filename,
                string path,
                Source source,
                Size size,
                string segmented,
                List<_Object> objects)//构造器
            {
                _folder = folder;
                _filename = filename;
                _path = path;
                _source = source;
                _size = size;
                _segmented = segmented;
                for (int i = 0; i < objects.Count; i++)
                {
                    _objects.Add(objects[i]);
                }
            }
            
            public class Source
            {
                public string _database;

                public Source(string database)//构造器
                {
                    _database = database;
                }
            }

            public class Size
            {
                public string _width;
                public string _height;
                public string _depth;

                public Size(string width,
                    string height,
                    string depth)//构造器
                {
                    _width = width;
                    _height = height;
                    _depth = depth;
                }
            }

            public class _Object
            {
                public string _name;
                public string _pose;
                public string _truncated;
                public string _difficult;
                public Bndbox _bndbox;

                public _Object(string name, 
                    string pose,
                    string truncated,
                    string difficult,
                    Bndbox bndbox)//构造器
                {
                    _name = name;
                    _pose = pose;
                    _truncated = truncated;
                    _difficult = difficult;
                    _bndbox = bndbox;
                }


                public class Bndbox
                {
                    public string _xmin;
                    public string _ymin;
                    public string _xmax;
                    public string _ymax;

                    public Bndbox(string xmin,
                        string ymin,
                        string xmax,
                        string ymax)//构造器
                    {
                        _xmin = xmin;
                        _ymin = ymin;
                        _xmax = xmax;
                        _ymax = ymax;
                    }
                }
            }
        }

        

        public static Dictionary<string, object> ToDictionary(object o)
        {
            if (o == null)
            {
                return null;
            }
            var fileds = o.GetType().GetFields();
            Dictionary<string,object> dic = new Dictionary<string, object>();
            
            foreach (var field in fileds)
            {
                if (field != null)
                {
                    if (field.FieldType == typeof(List<Annotation._Object>))
                    {
                        int i = 0;
                        foreach (var v in (field.GetValue(o) as List<Annotation._Object>))//遍历object列表
                        {
                            dic.Add((field.Name+i).Split('_')[1], ToDictionary(v));//将列表名+i作为字典的键名,将列表元素的值递归加入字典
                            i++;
                        }
                    }
                    else if (field.FieldType == typeof(string))
                    {
                        dic.Add(field.Name.Split('_')[1], field.GetValue(o));
//                        foreach (var kvp in dic)
//                        {
//                            Console.WriteLine($"{kvp.Key}:{kvp.Value}");
//                        }
                    }
                    else
                    {
                        dic.Add(field.Name.Split('_')[1], ToDictionary(field.GetValue(o)));
                    }
                }
            }
            return dic;
        }
        
    }
}

 

posted on 2020-08-12 10:26  Manuel  阅读(173)  评论(0编辑  收藏  举报