原文发布时间为: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();
        }
    }

最新文章

  1. 使用MongoDB和JSP实现一个简单的购物车系统
  2. 第二周02:Fusion ICP逐帧融合
  3. CentOS7安装Cobbler
  4. BZOJ 3680 吊打XXX
  5. 写代码中遇到的问题(php接收不到传过来的json数据,php使用utf8的用法)
  6. [SCOI 2016]美味
  7. 线程demo异常处理
  8. 一步一步和我学Apache JMeter
  9. @ResponseBody注解
  10. TODO 软件测试68题
  11. C/C++ 宏技巧
  12. ucos获得系统时间OSTimeGet();
  13. SpringCloud学习资料
  14. bzoj 1914: [Usaco2010 OPen]Triangle Counting 数三角形
  15. 设置PyCharm中的Python代码模版
  16. CIDR的IP地址的表示与划分方法
  17. Robot Framework——百度搜索
  18. 原创:MVC 5 实例教程(MvcMovieStore 新概念版:mvc5.0,EF6.01) - 1、初露锋芒
  19. 【Alpha】阶段第一次Scrum Meeting
  20. 没有为扩展名“.cshtml”注册的生成提供程序。

热门文章

  1. ASP.NET Core模块化前后端分离快速开发框架介绍之2、快速创建一个业务模块
  2. 绘制三角形:imageline()
  3. PHP 批量操作 Excel
  4. Union found
  5. ARM Linux内核源码剖析索引
  6. HDU1505-City Game(记忆化搜索)
  7. qt4.8.5 qtwebkit 静态编译 版本
  8. Java面向对象---方法递归调用
  9. MySQL之索引(一)
  10. php和js中数组的总结