设为首页 加入收藏

TOP

动态类型序列化(六)
2019-09-17 18:55:11 】 浏览:111
Tags:动态 类型 序列化
#endregion public enum Classify { Sample, List, Array, Dictionary, Dynamic, Custom, } public static List<XmlElement> GetChidlren(this XmlElement element) { return element.Cast<XmlElement>().ToList(); } }

  

  

核心思路就是递归,充分利用元数据

 

测试代码

 public class XmlConfig : IConfig
    {
        public string PathOrSourceString { get; set; }
        public dynamic Data { get; set; }
    }

    public interface ITestModel
    {

        string Name { get; set; }

        string DataType { get; set; }

        string Data { get; set; }


    }

    public class TestConfig
    {
        public ITestModel Model { get; set; }

        public  List<ITestModel> List { get; set; }
        public Dictionary<string, ITestModel> Dict { get; set; }
    }



    public class TestModel: ITestModel
    {
        public string Name { get; set; }

        public string DataType { get; set; }

        public string Data { get; set; }

        public List<ITestModel> List { get; set; }
        public Dictionary<string, ITestModel> Dict { get; set; }
    }


    public class ConfigTest
    {
        public static void PerformanceTest()
        {
            var xmlconfig = new XmlConfig();

            xmlconfig.PathOrSourceString = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Configs", "test1.Config");

            #region list 类,字典测试
            
            var testModel=   new TestModel()
            {
                Name = "1",
                DataType = "1",
                Data = "1",
                List = new List<ITestModel>
                {
                    new TestModel
                    {
                        Name = "2",
                        DataType = "2",
                        Data = "2",
                    },
                    new TestModel
                    {
                        Name = "3",
                        DataType = "3",
                        Data = "3",
                    },
                },
                Dict = new Dictionary<string, ITestModel>
                {
                    {"4", new TestModel
                            {
                                Name = "4",
                                DataType = "4",
                                Data = "4",
                            }
                     },
                    {"5", new TestModel
                            {
                                Name = "5",
                                DataType = "5",
                                Data = "5",
                            }
                     },
                }
            };
            #endregion

            xmlconfig.Data = new TestConfig()
            {
                Model = testModel,
                Dict = new Dictionary<string, ITestModel>()
                {
                    {"1",testModel },
                    {"2",testModel }
                },
                List = new List<ITestModel> { testModel,testModel}
            };


            #region 动态类型,类,list,字典总和测试

            xmlconfig.Data = new DynamicDictionary();

            xmlconfig.Data.Name = "Test1";
            xmlconfig.Data.DataType = "Test1";

            xmlconfig.Data.List = new List<TestModel>
            {
                new TestModel
                {
                    Name = "2",
                    DataType = "2",
                    Data = "2",
                },
                new TestModel
                {
                    Name = "3",
                    DataType = "3",
                    Data = "3",
                },
            };
            xmlconfig.Data.Dict = new Dictionary<string, TestModel>
            {
                {
                    "4", new TestModel
                    {
                        Name = "4",
                        DataType = "4",
                        Data = "4",
                    }
                },
                {
                    "5", new TestModel
                    {
                        Name = "5",
                        DataType = "5",
                        Data = "5",
                    }
                },
            };
            xmlconfig.Data.Other.Name = "Test1";
            xmlconfig.Data.Other.DataType = "Test1";

            #endregion

            xmlconfig.SaveToFile();


            var data = new XmlConfig();

            data.PathOrSourceString = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Configs", "test1.Config");
            data.LoadFromFile();
        }

 配置文件为

<Data Type="dynamic" Classify="
首页 上一页 3 4 5 6 7 下一页 尾页 6/7/7
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇理解依赖注入和控制反转 下一篇RefulApi自动化测试~Hitchhiker的..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目