csharp 动态Expando演示

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了csharp 动态Expando演示相关的知识,希望对你有一定的参考价值。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;
using System.Diagnostics;
using System.Dynamic;

namespace ConsoleApplication4
{
    class DynamicExpandoampleProgram
    {
        static void Main(string[] args)
        {
            //第一种写法
            Dictionary<string, object> dict = new Dictionary<string, object>();
            Dictionary<string, object> address = new Dictionary<string, object>();
            dict["Address"] = address;
            address["State"] = "china";
            Console.WriteLine(((Dictionary < string, object > )dict["Address"])["State"]);

            //第二种写法
            dynamic expando = new ExpandoObject();
            expando.address = new ExpandoObject();
            expando.address.state = "china";
            Console.WriteLine(expando.address.state);

            Console.Read();

        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;
using System.Diagnostics;

namespace ConsoleApplication4
{
    class DynamicProgram
    {
        static void Main(string[] args)
        {
            //这个是老式的写法
            var doc = XDocument.Load("Employees.xml");
            foreach (var element in doc.Element("Employees").Elements("Employee"))
            {
                Console.WriteLine(element.Element("FirstName").Value);
            }

            //这是用Dynamic的写法
            var doc2 = XDocument.Load("Employees.xml").AsExpando();
            foreach (var item in doc2.Employees)
            {
                Console.WriteLine(item.FirstName);
            }
        }
    }
    public static class ExpandoXml
    {
        public static dynamic AsExpando(this XDocument xDocument)
        {
            return CreateExpando(xDocument.Root);
        }

        private static dynamic CreateExpando(XElement xElement)
        {
            var result = new System.Dynamic.ExpandoObject() as IDictionary<string, object>;
            if (xElement.Elements().Any(x => x.HasElements))
            {
                var list = new List<System.Dynamic.ExpandoObject>();
                result.Add(xElement.Name.ToString(), list);
                foreach (var childElement in xElement.Elements())
                {
                    list.Add(CreateExpando(childElement));
                }
            }
            else
            {
                foreach (var leafElement in xElement.Elements())
                {
                    result.Add(leafElement.Name.ToString(), leafElement.Value);
                }
            }
            return result;
        }
    }
}

以上是关于csharp 动态Expando演示的主要内容,如果未能解决你的问题,请参考以下文章

动态查询 - Expando/动态对象类型

在 Django 模板中迭代 Expando 的动态属性

如何在 Google App Engine expando 类中创建动态字段?

Dart 的“Expando”功能是啥,它有啥作用?

python expando数据库Django GAE

如何让 ServiceStack 序列化/反序列化具有正确类型的 expando 对象