该类型可以实现的是动态添加属性和移除属性,有点类似 js 中对象的操作,非常灵活
1 static void Main(string[] args) 2 { 3 dynamic obj = new System.Dynamic.ExpandoObject(); 4 obj.name = "jj"; 5 obj.age = 10; 6 obj.gender = 1; 7 8 foreach (var item in (IDictionary<string,object>)obj) 9 { 10 Console.WriteLine(item.Key + " : " + item.Value); 11 } 12 13 Console.WriteLine("************************************************"); 14 15 //动态删除属性 16 var dic = (IDictionary<string,object>)obj; 17 dic.Remove("name"); 18 foreach (var item in (IDictionary<string, object>)obj) 19 { 20 Console.WriteLine(item.Key + " : " + item.Value); 21 } 22 23 Console.Read(); 24 }
显示结果:
时间: 2024-10-12 14:58:12