简体   繁体   English

将类型'System.Dynamic.DynamicObject转换为System.Collections.IEnumerable

[英]Convert type 'System.Dynamic.DynamicObject to System.Collections.IEnumerable

I'm successfully using the JavaScriptSerializer in MVC3 to de-serialize a json string in to a dynamic object. 我成功地使用MVC3中的JavaScriptSerializer将json字符串反序列化为动态对象。 What I can't figure out is how to cast it to something I can enumerate over. 我无法弄清楚的是如何将它投射到我可以列举的东西上。 The foreach line of code below is my latest attemt but it errors with: "Cannot implicitly convert type 'System.Dynamic.DynamicObject' to 'System.Collections.IEnumerable'. How can I convert or cast so that I can iterate through the dictionary? 下面的foreach代码行是我最新的attemt,但它错误:“无法将类型'System.Dynamic.DynamicObject'隐式转换为'System.Collections.IEnumerable'。如何转换或转换以便我可以遍历字典?

 public dynamic GetEntities(string entityName, string entityField)
        {
           var serializer = new JavaScriptSerializer();
            serializer.RegisterConverters(new[] { new                        MyProject.Extensions.JsonExtension.DynamicJsonConverter() });
           dynamic data = serializer.Deserialize(json, typeof(object));
           return data;
        }


 foreach (var author in GetEntities("author", "lastname"))

Given your example usage of 'GetEntities', try changing its return type to IEnumerable<T> (or, although strongly not recommended, at least an IEnumerable<dynamic> ). 鉴于您使用'GetEntities'的示例,请尝试将其返回类型更改为IEnumerable<T> (或者,尽管强烈建议不要使用,但至少要使用IEnumerable<dynamic> )。 You would need to do some filtering within the method to extract the appropriate entities based on the 'entityName' input parameter. 您需要在方法中进行一些过滤,以根据'entityName'输入参数提取适当的实体。 Although, it's unclear what the intended usage is of the other input parameter ('entityField'). 虽然,目前还不清楚其他输入参数('entityField')的用途是什么。

DynamicObject is inherited from IDictionary, so you can cast it to IDictionary. DynamicObject继承自IDictionary,因此您可以将其强制转换为IDictionary。

public IDictionary<string, object> GetEntities(string entityName, string entityField)
    {
       var serializer = new JavaScriptSerializer();
        serializer.RegisterConverters(new[] { new MyProject.Extensions.JsonExtension.DynamicJsonConverter() });
       dynamic data = serializer.Deserialize(json, typeof(object));
       return data as IDictionary<string, object>;
    }




foreach (var author in GetEntities("author", "lastname"))

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 天空驱动器System.Dynamic.DynamicObject - skydrive System.Dynamic.DynamicObject 无法转换为&#39;System.Collections.IEnumerable - cannot convert to 'System.Collections.IEnumerable 无法将带有[]的索引应用于“System.Dynamic.DynamicObject”类型的表达式 - Cannot apply indexing with [] to an expression of type 'System.Dynamic.DynamicObject' 类 System.Dynamic.DynamicObject .. 为什么不抽象? - Class System.Dynamic.DynamicObject .. Why not abstract? LINQ for System.Collections.IEnumerable - LINQ for System.Collections.IEnumerable 无法初始化类型xy不实现&#39;System.Collections.IEnumerable&#39; - cannot initialise type xy Does not implement 'System.Collections.IEnumerable' C# 无法将类型“XXXX.Classes.Idophalen”隐式转换为“System.Collections.IEnumerable” - C# Cannot implicitly convert type 'XXXX.Classes.Idophalen' to 'System.Collections.IEnumerable' &#39;System.Dynamic.DynamicObject&#39;不包含&#39;PropertyName&#39;的定义 - 'System.Dynamic.DynamicObject' does not contain a definition for 'PropertyName' 无法将类型“project_name.Models.model_name”隐式转换为“System.Collections.IEnumerable”。 存在显式转换 - Cannot implicitly convert type 'project_name.Models.model_name' to 'System.Collections.IEnumerable'. An explicit conversion exists 无法将类型为“System.Management.Automation.PSCustomObject”的对象强制转换为“System.Collections.IEnumerable” - Unable to cast object of type 'System.Management.Automation.PSCustomObject' to type 'System.Collections.IEnumerable'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM