C#-----ExpandoObject動態屬性

anqi771129發表於2019-07-26
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Dynamic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp2
{
    class Program
    {
        static void Main(string[] args)
        {
            dynamic mileageObj = new ExpandoObject();
            mileageObj.userName = "李斯";
            mileageObj.userAge = 23;
            for (int i = 1; i <= 5; i++)
            {
                (mileageObj as ICollection<KeyValuePair<string, object>>).Add(new KeyValuePair<string, object>("動態屬性" + i, "動態屬性" + i));
            }

            Console.WriteLine(JsonConvert.SerializeObject(mileageObj));
            Console.ReadLine();
        }
    }
}

 

轉載於:https://www.cnblogs.com/fengfuwanliu/p/11250546.html

相關文章