动态地向动态对象添加属性? [英] Dynamically adding properties to a dynamic object?

查看:100
本文介绍了动态地向动态对象添加属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个

dynamic d = new ExpandoObject();
d.Name = attribute.QualifiedName.Name;

所以,我知道d将有一个属性名称。现在如果我在编译时不知道该属性的名称,我该如何将该属性添加到动态。
i发现此 SO问题

so , i know that d will have a property Name. Now if i don't know the name of the property at compile time , how do i add that property to the dynamic. i found this SO Question

所以,这是一个复杂的调用绑定器的概念等等,这是很难得到的第一个地方。这样做更简单的方法?

so, there is this complicated concept of call binders etc..which is tough to get in the first place.any simpler way of doing this ?

推荐答案

dynamic d = new ExpandoObject();
((IDictionary<string,object>)d)["test"] = 1;
//now you have d.test = 1

这篇关于动态地向动态对象添加属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆