[英]“'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日志,并将看到的异常条目配对:
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.Count
到root.clients.Count
中for
循环。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.