I'm trying to read values from System.Web.Helpers.DynamicJsonObject. I can see the values in the debugger but I can't figure out how to access them. I have tried this
item.GetType().GetProperty("batch_id").GetValue(item, null);
but when I try that I get this response in the debugger "item.GetType().GetProperty("batch_id")' is null"
I have attached a picture from my solution
Thank you, -Tesh
In C#, you can access dynamic properties by obtaining a PropertyObject reference from the specific object reference using the AsPropertyObject method on the object.
The ExpandoObject class enables you to add and delete members of its instances at run time and also to set and get values of these members. This class supports dynamic binding, which enables you to use standard syntax like sampleObject. sampleMember instead of more complex syntax like sampleObject.
Dynamic objects expose members such as properties and methods at run time, instead of at compile time. This enables you to create objects to work with structures that do not match a static type or format.
Dynamic types are similar to object types except that type checking for object type variables takes place at compile time, whereas that for the dynamic type variables takes place at runtime.
It is dynamic so you can just do:
string batchId = item.batch_id;
If for some reason you have the property name in a string
, and don't know it at compile time, the indexing operator will work:
string value = item["batch_id"];
Try enumerating the values DynamicJsonObject.GetDynamicMemberNames Method. It returns an IEnumerable of string.
It doesn't work because they are fields, not properties. And, yeah, it is dynamic, so you can use just item.batch_id
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With