从动态对象C#中读取值

时间:2022-09-25 08:58:38

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

我正在尝试从System.Web.Helpers.DynamicJsonObject中读取值。我可以在调试器中看到这些值,但我无法弄清楚如何访问它们。我试过这个

item.GetType().GetProperty("batch_id").GetValue(item, null);

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"

但是当我尝试在调试器中得到这个响应“item.GetType()。GetProperty(”batch_id“)'为null”

I have attached a picture from my solution 从动态对象C#中读取值

我附上了我的解决方案中的图片

Thank you, -Tesh

谢谢,-Tesh

3 个解决方案

#1


24  

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"];

#2


3  

Try enumerating the values DynamicJsonObject.GetDynamicMemberNames Method. It returns an IEnumerable of string.

尝试枚举值DynamicJsonObject.GetDynamicMemberNames方法。它返回一个IEnumerable字符串。

#3


1  

It doesn't work because they are fields, not properties. And, yeah, it is dynamic, so you can use just item.batch_id.

它不起作用,因为它们是字段,而不是属性。而且,是的,它是动态的,所以你可以只使用item.batch_id。

#1


24  

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"];

#2


3  

Try enumerating the values DynamicJsonObject.GetDynamicMemberNames Method. It returns an IEnumerable of string.

尝试枚举值DynamicJsonObject.GetDynamicMemberNames方法。它返回一个IEnumerable字符串。

#3


1  

It doesn't work because they are fields, not properties. And, yeah, it is dynamic, so you can use just item.batch_id.

它不起作用,因为它们是字段,而不是属性。而且,是的,它是动态的,所以你可以只使用item.batch_id。