问题
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