从Expando /动态对象动态删除成员 [英] Dynamically removing a member from Expando /dynamic object
问题描述
我正在寻找一种从动态对象动态删除成员的方法(可以在这里使用Expando对象吗?)。
I'm looking for a way to remove members dynamically from an dynamic object (may be can we use Expando object here?).
好的,我想有一点澄清是需要的...
OK, I guess a little clarification is needed...
当你这样做:
dynamic foo = new ExpandoObject();
foo.Bar = 42;
foo.Jar = 25;
Bar
,'jar'可以在运行时动态添加。我将其显示给用户,他可以进一步添加自己的属性(表单可用选项),这可以通过向动态对象添加成员来完成。
The Bar
, 'jar' property will be added dynamically at runtime. I am displaying this to user , he may further add his own properties (form available option), this can be done by just adding member to dynamic object.
但他也可以从Foo对象中删除成员(称为Jar),我们如何删除成员形式的动态对象?我们可以在这里使用Expando对象吗?
But he can also remove members (say 'Jar') form the Foo object, how do we remove the member form dynamic object? Can we use Expando object here?
推荐答案
您可以将 ExpandoObject
作为 IDictionary< string,object>
,然后删除这样:
You can treat the ExpandoObject
as an IDictionary<string, object>
instead, and then remove it that way:
IDictionary<string, object> map = foo;
map.Remove("Jar");
这篇关于从Expando /动态对象动态删除成员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!