Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

How to create own dynamic type or dynamic object?


Asked by Kaleb Walsh on Dec 02, 2021 FAQ



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 this object, which is cool .I want to use something like that, beyond MVC application and Controller class in other types of applications.
Next,
In C#, you specify the type of a late-bound object as dynamic. In Visual Basic, you specify the type of a late-bound object as Object. For more information, see dynamic and Early and Late Binding. You can create custom dynamic objects by using the classes in the System.Dynamic namespace.
In this manner, The DynamicObject class enables you to define which operations can be performed on dynamic objects and how to perform those operations. For example, you can define what happens when you try to get or set an object property, call a method, or perform standard mathematical operations such as addition and multiplication.
Thereof,
This enables you to create objects to work with structures that do not match a static type or format. For example, you can use a dynamic object to reference the HTML Document Object Model (DOM), that can contain any combination of valid HTML markup elements and attributes.
Also,
You can dynamically create classes, which inherits Foo, with any additional properties. Thus you can add instances of those dynamic classes into List<Foo>. Adding property for a class in runtime, it is not possible to perform. Thanks for contributing an answer to Stack Overflow!