问题描述
我正在尝试了解DynamicObject类型。发现此MSDN文章非常简洁明了,如何创建和使用DynamicObject:
I'm trying to understand the DynamicObject type. Found this MSDN article to be very consise and clear as how to create and use DynamicObject:
本文包含一个从DynamicObject继承的简单DynamicDictionary类。
The article contains a simple DynamicDictionary class that inherits from DynamicObject.
现在,我要遍历动态创建的DynamicObject属性:
Now I want to iterate over my dynamically created DynamicObject properties:
dynamic d = new DynamicDictionary();
d.Name = "Myname";
d.Number = 1080;
foreach (var prop in d.GetType().GetProperties())
{
Console.Write prop.Key;
Console.Write prop.Value;
}
显然这是行不通的。我想学习如何在不更改DynamicDictionary类的情况下执行此操作,因为我实际上是在尝试学习如何将其用于从DynamicObject继承的所有现有对象。
Obviously that does not work. I want to learn how to do this without a change in my DynamicDictionary class, since I'm really trying to learn how to use this for all kinds of existing objects that inherits from DynamicObject.
需要反思吗?我一定缺少一些东西...
Is Reflection needed? I must be missing something...
推荐答案
您是否尝试过DynamicDictionary.GetDynamicMemberNames()方法? -
Did you tried DynamicDictionary.GetDynamicMemberNames() method? - http://msdn.microsoft.com/en-us/library/system.dynamic.dynamicobject.getdynamicmembernames.aspx
这篇关于循环DynamicObject属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!