在运行时将成员添加到动态对象 [英] Adding members to a dynamic object at runtime

查看:131
本文介绍了在运行时将成员添加到动态对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在探索.NET 4.0中的DynamicObject模型。该应用程序是通过某种文本/ xml文件描述对象的应用程序,程序必须在读取该文件时创建一个对象。



使用DynamicObject,我们可以轻松添加成员,因为我们先知道成员的名字。但是,如果我们甚至不知道要添加的成员的名称怎么办?有没有办法使这个动态呢?



例如,我需要创建一个包含成员Property1,Property2的对象,另一个对象具有PropertyA和PropertyB,如文本/ XML文件。如何根据这个信息动态创建一个对象?



更新
我从这篇文章中得到了一些想法: http://www.codeproject.com/KB/cs/dynamicincsharp.aspx



此实现允许我执行如下操作:

  dynamic d =新的PFDynamicChannel(); 
PFCouplings c =((PFChannel)d).Coupling;
d.NewProperty =X;

我不想使用字典的原因是使用TryGetMember和TrySetMember方法,我可以覆盖,我可以在其中提出对程序至关重要的事件。



这样,我可以继承基类(PFChannel),但是我也可以在飞行中添加成员。但是,我的问题是,直到运行时才会知道新的属性名称。而且,我实际上并不认为动态对象可以让我快速添加新的属性。如果是这样的话,我如何利用ExpandoObject来给我这个能力?

解决方案

需要这样做,您应该看看 ExpandoObject 。如果你需要这样做,那么仍然使用 DynamicObject ,你需要编写代码来记住属性值,基本上你可以使用嵌入式 ExpandoObject



以后,我不清楚你想对这个对象做什么 - 你确定你需要动态打字吗?一个字典< string,object> 实际上会更糟吗?这取决于基本上将要消费的对象。


I am exploring the DynamicObject model in .NET 4.0. The application is one where an object will be described through some sort of text/xml file, and the program must create an object upon reading that file.

With DynamicObject, we can add members easily, given that we know the name of the member a priori. But, what if we don't even know the name of the member to add? Is there a way to make that dynamic as well?

For example, say I need to create an object with members 'Property1', 'Property2', and another object with 'PropertyA', and 'PropertyB' as described by the text/XML file. How can I create an object dynamically based on this info?

UPDATE I got some ideas from this post: http://www.codeproject.com/KB/cs/dynamicincsharp.aspx

This implementation allows me to do something like the following:

dynamic d = new PFDynamicChannel();
PFCouplings c = ((PFChannel)d).Coupling;
d.NewProperty = "X";

The reason I do not wish to use a dictionary is to make use of TryGetMember, and TrySetMember methods that I can override, within which I can raise events that are essential for the program.

This way, I can inherit from a base class (PFChannel), but I can also add members on the fly. But, my problem is that I will not know the new property name until runtime. And, I actually don't think the dynamic object allows me to add new properties on the fly. If this is the case, how can I make use of ExpandoObject to give me this ability?

解决方案

If you only need to do that, you should look at ExpandoObject. If you need to do that and still use DynamicObject, you will need to write code to remember property values, basically... which you could potentially do with an embedded ExpandoObject.

It's not clear to me what you want to do with this object afterwards though - are you sure you need dynamic typing at all? Would a Dictionary<string, object> actually be any worse? It depends what's going to consume the object later basically.

这篇关于在运行时将成员添加到动态对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆