Dynamically adding members to a dynamic object

前端 未结 4 1389
野性不改
野性不改 2020-12-05 21:07

I\'m looking for a way to add members dynamically to an dynamic object. OK, I guess a little clarification is needed...

When you do that :

dynamic fo         


        
4条回答
  •  伪装坚强ぢ
    2020-12-05 21:50

    ExpandoObject implements IDictionary albeit explicitly. What this means is that you can simply cast the ExpandoObject to IDictionary and manipulate the dictionary.

    dynamic foo = new ExpandoObject();
    foo.Bar = 42;
    food = (IDictionary)foo;
    food["Baz"] = 54
    

提交回复
热议问题