循环 DynamicObject 属性
我试图理解 DynamicObject 类型。发现这篇 MSDN 文章对于如何创建和使用 DynamicObject 非常简洁明了:
http://msdn.microsoft.com/en-us/library/system.dynamic.dynamicobject.aspx
本文包含一个继承自 DynamicObject 的简单 DynamicDictionary 类。
现在我想迭代动态创建的 DynamicObject 属性:
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 继承的各种现有对象。
是否需要反思?我一定是错过了什么……
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:
http://msdn.microsoft.com/en-us/library/system.dynamic.dynamicobject.aspx
The article contains a simple DynamicDictionary class that inherits from 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;
}
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...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我相信您会对
ExpandoObject
类。
DynamicObject
类只是一个基础,您可以在其中提供所有逻辑。它显式实现了IDictionary
接口,以便您可以访问它的属性或以这种方式添加新属性。我刚刚意识到您正在实现 DynamicDictionary 类并误解了您的问题。对不起。
与动态引用一起使用时,您只能访问公开暴露的成员。由于只有
Count
被声明为公共(除了其他DynamicObject
成员),因此您需要使用反射来访问内部字典以轻松获取这些值(如果您不打算进行任何进一步的更改)。I believe you'd be interested in the
ExpandoObject
class. TheDynamicObject
class is just a base where you're meant to provide all the logic. It explicitly implements theIDictionary<string, object>
interface so you can access it properties or add new ones that way.I had just realized that you are implementing the
DynamicDictionary
class and misunderstood your question. Sorry.When used with a dynamic reference, you only have access to publicly exposed members. Since only
Count
is declared public (besides the otherDynamicObject
members), you'd need to use reflection in order to access the inner dictionary to easily get those values (if you don't intend to make any further changes).您是否尝试过 DynamicDictionary.GetDynamicMemberNames() 方法? - http://msdn.microsoft.com/en-我们/library/system.dynamic.dynamicobject.getdynamicmembernames.aspx
Did you tried DynamicDictionary.GetDynamicMemberNames() method? - http://msdn.microsoft.com/en-us/library/system.dynamic.dynamicobject.getdynamicmembernames.aspx
我知道这个主题已经很老了,但对于那些仍然想知道如何做到这一点的人来说,为什么不使用
TryGetMember()
方法,而是构造一个继承GetMemberBinder
的类,例如:然后使用 LINQ:
I know this topic is pretty old but for those who are still wondering on how to do it, why not use the
TryGetMember()
method but construct a class that inherits theGetMemberBinder
, like:Then with LINQ: