该类型可以实现的是动态添加属性和移除属性,有点类似 js 中对象的操作,非常灵活
static void Main(string[] args)
{
dynamic obj = new System.Dynamic.ExpandoObject();
obj.name = "jj";
obj.age = ;
obj.gender = ; foreach (var item in (IDictionary<string,object>)obj)
{
Console.WriteLine(item.Key + " : " + item.Value);
} Console.WriteLine("************************************************"); //动态删除属性
var dic = (IDictionary<string,object>)obj;
dic.Remove("name");
foreach (var item in (IDictionary<string, object>)obj)
{
Console.WriteLine(item.Key + " : " + item.Value);
} Console.Read();
}
显示结果: