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

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.

+121
c # dynamic viewbag
Oct 03 '12 at 13:17
source share
5 answers
 dynamic MyDynamic = new System.Dynamic.ExpandoObject(); MyDynamic.A = "A"; MyDynamic.B = "B"; MyDynamic.C = "C"; MyDynamic.Number = 12; MyDynamic.MyMethod = new Func<int>(() => { return 55; }); Console.WriteLine(MyDynamic.MyMethod()); 

More on the ExpandoObject class and optional patterns: Represents an object whose members can be dynamically added and deleted at run time.

+258
Oct 03 '12 at 13:21
source share

ExpandoObject is what you are 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; 
+32
03 Oct '12 at 13:21
source share

Recently, I needed to take another step, which was to make additions of properties in a dynamic object, dynamic themselves, based on user records. The examples here and from the Microsoft ExpandoObject documentation do not take into account the features of dynamically adding add- ons , but you can assume how you list and delete properties. Anyway, I thought it might be useful to someone. Here is a very simplified version of how to add truly dynamic properties to ExpandoObject (ignoring the keyword and other processing):

  // my pretend dataset List<string> fields = new List<string>(); // my 'columns' fields.Add("this_thing"); fields.Add("that_thing"); fields.Add("the_other"); dynamic exo = new System.Dynamic.ExpandoObject(); foreach (string field in fields) { ((IDictionary<String, Object>)exo).Add(field, field + "_data"); } // output - from Json.Net NuGet package textBox1.Text = Newtonsoft.Json.JsonConvert.SerializeObject(exo); 
+29
Aug 29 '16 at
source share

You can use the ExpandoObject Class , which is located in the System.Dynamic namespace.

 dynamic MyDynamic = new ExpandoObject(); MyDynamic.A = "A"; MyDynamic.B = "B"; MyDynamic.C = "C"; MyDynamic.SomeProperty = SomeValue MyDynamic.number = 10; MyDynamic.Increment = (Action)(() => { MyDynamic.number++; }); 

Additional information is available at ExpandoObject MSDN.

+11
Oct 03
source share
 dynamic MyDynamic = new ExpandoObject(); 
+7
03 Oct
source share



All Articles