Asp.net dynamic和匿名对象的使用

dynamic

            dynamic modelCompany = new ExpandoObject();
            modelCompany.Name = "Oschina";
            modelCompany.Address = "深圳";

            dynamic modelInfo = new ExpandoObject();
            modelInfo.Name = "红薯光头";
            modelInfo.Age = 66;
            modelInfo.Company = modelCompany;

//返回
{
    "Name": "红薯光头",
    "Age": 66,
    "Company": {
        "Name": "Oschina",
        "Address": "深圳"
    }
}

匿名对象

var personCompany = new[]
            {
                new
                {
                    Name = "Oschina",
                    Address = "深圳"
                }
            };
            var personInfo = new[]
            {
                new
                {
                    Name = "红薯光头",
                    Age = 56,
                    Company = personCompany
                }
            };
            var model = personInfo.ToList();
            model.Add(new { Name = "大菲", Age = 18, Company = personCompany });

//返回
[
    {
        "Name": "红薯光头",
        "Age": 56,
        "Company": [
            {
                "Name": "Oschina",
                "Address": "深圳"
            }
        ]
    },
    {
        "Name": "大菲",
        "Age": 18,
        "Company": [
            {
                "Name": "Oschina",
                "Address": "深圳"
            }
        ]
    }
]

谢谢

posted @ 2016-08-11 09:53  游子善心  阅读(2)  评论(0编辑  收藏  举报  来源