Here, for example, the ViewBag is a property of the ControllerBase class, and we can dynamically get / set values ββand add any number of additional fields or properties for this object. This is cool. I want to use something similar besides the MVC application and the Controller class in other types of applications. When I tried to create a dynamic object and set it as:
1. dynamic MyDynamic = new { A="a" }; 2. MyDynamic.A = "asd"; 3. Console.WriteLine(MyDynamic.A);
I have a RuntimeBinderException with the message Property or indexer '<> f__AnonymousType0.A' cannot be assigned - it is read-only on line 2. Also, I suggest it is not quite what I'm looking for. Maybe there is a class that can allow me to do something like:
??? MyDynamic = new ???(); MyDynamic.A = "A"; MyDynamic.B = "B"; MyDynamic.C = DateTime.Now; MyDynamic.TheAnswerToLifeTheUniverseAndEverything = 42;
with dynamic properties for adding and customizing.
c # dynamic viewbag
Dmytro Oct 03 '12 at 13:17 2012-10-03 13:17
source share