How to iterate over an array of dynamic type in C# 4.0?

半腔热情 提交于 2020-01-24 02:32:31

问题


Hi I have an array of dynamic type which i want to iterate through. But when i say arrayObject.lenght, i get following error:

'object' does not contain a definition for 'length' and no extension method 'length' accepting a first argument of type 'object' could be found

how do i iterate over the array?

[Update]

I post a custom json object using jquery ajax and i have written a model binder for untyped JSON. Here is a screenshot:


回答1:


Found the solution:

foreach (dynamic item in cartJsonObject)
{
   // code here
}

I was trying with for loop and foreach using var type. Changing it to dynamic solved it.




回答2:


This error very likely means that what you have is not an array but another collection - IEnumerable would be my best guess. You can use the Count() extension method provided by Linq to retrieve the number of elements.



来源:https://stackoverflow.com/questions/8565619/how-to-iterate-over-an-array-of-dynamic-type-in-c-sharp-4-0

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!