• Dictionary To Dynamic


    原文发布时间为:2012-12-25 —— 来源于本人的百度文章 [由搬家工具导入]

    public static class DictionaryExt
        {

            /// <summary>
            /// Extension method that turns a dictionary of string and object to an ExpandoObject
            /// </summary>
            public static ExpandoObject ToExpando(this IDictionary<string, object> dictionary)
            {
                var expando = new ExpandoObject();
                var expandoDic = (IDictionary<string, object>)expando;

                // go through the items in the dictionary and copy over the key value pairs)
                foreach (var kvp in dictionary)
                {
                    // if the value can also be turned into an ExpandoObject, then do it!
                    if (kvp.Value is IDictionary<string, object>)
                    {
                        var expandoValue = ((IDictionary<string, object>)kvp.Value).ToExpando();
                        expandoDic.Add(kvp.Key, expandoValue);
                    }
                    else if (kvp.Value is ICollection)
                    {
                        // iterate through the collection and convert any strin-object dictionaries
                        // along the way into expando objects
                        var itemList = new List<object>();
                        foreach (var item in (ICollection)kvp.Value)
                        {
                            if (item is IDictionary<string, object>)
                            {
                                var expandoItem = ((IDictionary<string, object>)item).ToExpando();
                                itemList.Add(expandoItem);
                            }
                            else
                            {
                                itemList.Add(item);
                            }
                        }

                        expandoDic.Add(kvp.Key, itemList);
                    }
                    else
                    {
                        expandoDic.Add(kvp);
                    }
                }

                return expando;
            }
        }



    ===============Demo==============

     public class Program
        {
            static void Main(string[] args)
            {
                var model = new Dictionary<string, object> { { "Name", "Jack" }, { "Age", 13 }, { "Married", false },
                    {"Girlfriend",new Dictionary<string, object> { { "Name", "Lucy" }, { "Age", 13 }, { "Married", true }}}
                };
                dynamic result = model.ToExpando();
                Console.WriteLine(result.Name);
                Console.WriteLine(result.Girlfriend.Name);
                Console.ReadLine();
            }
        }

  • 相关阅读:
    第六章 (循环结构二)
    MySQL改密码
    第八章
    第二章 变量 数据类型和运算符
    [Windows Phone 7璀璨]北漂1.0在线歌词播放器一.项目搭建及版权声明
    [Windows Phone 7璀璨]北漂1.0在线歌词播放器二.歌曲按艺术家分类实现
    [这不是Windows Phone 7]FitnessTrackerPlus(健身)一.数据库介绍
    [学习笔记]Silverlight4 RIA 开发全程解析[项目全程记录]第二章为站点流行而准备:提供一个可扩展的体系结构(写作中)
    [这不是Windows Phone 7]FitnessTrackerPlus(健身)二.架构搭建
    ERP成本核算
  • 原文地址:https://www.cnblogs.com/handboy/p/7182593.html
Copyright © 2020-2023  润新知