简体   繁体   English

““ System.Dynamic.ExpandoObject”不包含“ PropertyName”的定义

[英]“'System.Dynamic.ExpandoObject' does not contain a definition for ”PropertyName"

I have the following code that generates Dynamic object from XML file: 我有以下代码可从XML文件生成动态对象:

C# C#

    private static List<dynamic> GetClientObject()
    {
        var xDoc = XDocument.Load(new StreamReader(xmlPath + @"\client.xml"));
        dynamic root = new ExpandoObject();
        XmlToDynamic.Parse(root, xDoc.Elements().First());
        List<dynamic> clients = new List<dynamic>();

        for (int i = 0; i < root.clients.client.Count; i++)
        {
            clients.Add(new ExpandoObject());
            clients[i].Id = root.clients.client[i].id;
            clients[i].Name = root.clients.client[i].name;
            List<string> list = new List<string>();

            for (int j = 0; j < root.clients.client[i].emails.email.Count; j++)
            {
                list.Add(root.clients.client[i].emails.email[j].ToString());
            }

            clients[i].Email = string.Join(",", list);
        }
        return clients;
    }

XML XML

<clients>
    <client>
        <id>SomeId</id>
        <name>SomeName</name>
        <emails>
            <email>abc@xyz.com</email>
            <email>def@xyz.com</email>
            <email>ghi@xyz.com</email>
        </emails>
        <timezone>Mountain Standard Time</timezone>
    </client>
</clients>

The code works fine but I always see the following Exception(multiple times) in the IntelliTrace: 该代码工作正常,但我总是在IntelliTrace中看到以下异常(多次):

Exception:Thrown: "'System.Dynamic.ExpandoObject' does not contain a definition for 'client'" (Microsoft.CSharp.RuntimeBinder.RuntimeBinderException) A Microsoft.CSharp.RuntimeBinder.RuntimeBinderException was thrown: "'System.Dynamic.ExpandoObject' does not contain a definition for 'client'" 异常:抛出:“'System.Dynamic.ExpandoObject'不包含'客户端'的定义”。(Microsoft.CSharp.RuntimeBinder.RuntimeBinderException)抛出了Microsoft.CSharp.RuntimeBinder.RuntimeBinderException:不包含“客户”的定义”

Is there anything wrong with my code? 我的代码有什么问题吗?

I gather this is expected behavior when using an ExpandoObject . 我收集使用ExpandoObject时的预期行为。 I took a look at the IntelliTrace log for this code and the entries for the exceptions you are seeing are paired up: 我查看了此代码的IntelliTrace日志,并将看到的异常条目配对:

  • Exception: Thrown : "'System.Dynamic.ExpandoObject' does not contain a definition for 'clients'" (Microsoft.CSharp.RuntimeBinder.RuntimeBinderException) 异常: 抛出 :“'System.Dynamic.ExpandoObject'不包含'客户端'的定义”(Microsoft.CSharp.RuntimeBinder.RuntimeBinderException)
  • Exception: Caught : "'System.Dynamic.ExpandoObject' does not contain a definition for 'clients'" (Microsoft.CSharp.RuntimeBinder.RuntimeBinderException) 异常: 捕获 :“'System.Dynamic.ExpandoObject'不包含'客户端'的定义”(Microsoft.CSharp.RuntimeBinder.RuntimeBinderException)

ie The exception is being thrown and then caught. 即,异常被引发然后被捕获。 If you look at the Call Stack Window you will see that the throws and catches are within the .NET Framework. 如果您查看“调用堆栈”窗口,您将看到在.NET Framework中引发了异常。

BTW, I did have to make a small change to your code to make it run: I changed: root.clients.client.Count to root.clients.Count in the for loop. 顺便说一句,我也不得不做出一个小的改变了代码,使其运行:我改变: root.clients.client.Countroot.clients.Countfor循环。

暂无
暂无

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

相关问题 “System.Dynamic.ExpandoObject”不包含 Specflow C# 中“用户名”的定义 - 'System.Dynamic.ExpandoObject' does not contain a definition for 'UserName' in Specflow C# &#39;System.Dynamic.ExpandoObject&#39;不包含名称为&#39;Name&#39;的属性 - 'System.Dynamic.ExpandoObject' does not contain a property with the name 'Name' &#39;System.Dynamic.DynamicObject&#39;不包含&#39;PropertyName&#39;的定义 - 'System.Dynamic.DynamicObject' does not contain a definition for 'PropertyName' 更改System.Dynamic.ExpandoObject的默认行为 - Change System.Dynamic.ExpandoObject default behavior &#39;ExpandoObject&#39;不包含&#39;PropertyChanged&#39;的定义 - 'ExpandoObject' does not contain a definition for 'PropertyChanged' 类型“ System.Dynamic.ExpandoObject”的表达式不能用于类型“ System.Linq.IQueryable”的参数 - Expression of type 'System.Dynamic.ExpandoObject' cannot be used for parameter of type 'System.Linq.IQueryable` 如何在 C# 中将 T 类型的 model 转换为“System.Dynamic.ExpandoObject”? - How to cast a model of T type to 'System.Dynamic.ExpandoObject' in C#? MVC 3 Razor模型未更新:“模型”不包含“ PropertyName”的定义 - MVC 3 Razor model not updating: 'Model' does not contain a definition for 'PropertyName' &#39;System.Type&#39;不包含带有(dynamic)的&#39;GenericTypeArguments&#39;的定义 - 'System.Type' does not contain a definition for 'GenericTypeArguments' with (dynamic) Dynamic 不包含 GetType() 的定义 - Dynamic does not contain a definition for GetType()
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM