在C#中,如何从ExpandoObject中删除属性? [英] In C#, how do I remove a property from an ExpandoObject?

查看:294
本文介绍了在C#中,如何从ExpandoObject中删除属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

说我有这个对象:

dynamic foo = new ExpandoObject();
foo.bar = "fizz";
foo.bang = "buzz";

例如,如何删除foo.bang?

我不想简单地将属性的值设置为null,出于我的目的,我需要将其完全删除.另外,我意识到我可以通过从第一个绘制kv对来创建一个全新的ExpandoObject,但这效率很低.

I don't want to simply set the property's value to null--for my purposes I need to remove it altogether. Also, I realize that I could create a whole new ExpandoObject by drawing kv pairs from the first, but that would be pretty inefficient.

推荐答案

将Expando投射到IDictionary<string, object>并调用

Cast the expando to IDictionary<string, object> and call Remove:

var dict = (IDictionary<string, object>)foo;
dict.Remove("bang");

这篇关于在C#中,如何从ExpandoObject中删除属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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