如何在C#4.0中遍历动态类型的数组? [英] How to iterate over an array of dynamic type in C# 4.0?
问题描述
我有一个要迭代的动态类型数组。但是当我说arrayObject.lenght时,出现以下错误:
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'不包含'length'的定义,也没有扩展方法'length'接受第一个可以找到类型为'object'的参数
'object' does not contain a definition for 'length' and no extension method 'length' accepting a first argument of type 'object' could be found
我如何遍历数组?
[更新]
我使用jquery ajax发布了一个自定义json对象,并为未类型化的JSON编写了模型绑定程序。这是屏幕截图:
I post a custom json object using jquery ajax and i have written a model binder for untyped JSON. Here is a screenshot:
推荐答案
找到了解决方案:
foreach (dynamic item in cartJsonObject)
{
// code here
}
我尝试使用 var <和
for
循环和 foreach
/ code>类型。将其更改为动态
即可解决。
I was trying with for
loop and foreach
using var
type. Changing it to dynamic
solved it.
这篇关于如何在C#4.0中遍历动态类型的数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!