dynamic 如果接收的是匿名对象,是无法为属性赋值的,而如果是接收的定义对象,又无法扩展字段。 解决办法序列化为json字符串,然后用Dictionary反序列化,就能赋值了。也能扩展新的字段。 代码:
dynamic obj = new { a = 1, b = 2,hh=3 }; dynamic obj2 = new { obj1 = obj, a3 = 123 }; var str = JsonConvert.SerializeObject(obj2); var dic = JsonConvert.DeserializeObject<Dictionary<string, object>>(str); int a = dic["obj1"].a; dic["obj1"].a+=1; dic["ooo"] = "3_=";
标签:obj1,C#,dynamic,dic,序列化,赋值 From: https://www.cnblogs.com/HelloQLQ/p/17128228.html