How to create own dynamic type or dynamic object in C#?

前端 未结 7 1400
醉话见心
醉话见心 2020-12-04 08:05

There, is for example, ViewBag property of ControllerBase class and we can dynamically get/set values and add any number of additional fields or properties to t

7条回答
  •  春和景丽
    2020-12-04 08:44

    ExpandoObject is what are you looking for.

    dynamic MyDynamic = new ExpandoObject(); // note, the type MUST be dynamic to use dynamic invoking.
    MyDynamic.A = "A";
    MyDynamic.B = "B";
    MyDynamic.C = "C";
    MyDynamic.TheAnswerToLifeTheUniverseAndEverything = 42;
    

提交回复
热议问题