Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In C#, how do I remove a property from an ExpandoObject?

Say I have this object:

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

How would I remove foo.bang for example?

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.

like image 461
Matt Cashatt Avatar asked Jan 23 '13 23:01

Matt Cashatt


People also ask

What does != Mean in C?

The not-equal-to operator ( != ) returns true if the operands don't have the same value; otherwise, it returns false .

What is '~' in C programming?

In mathematics, the tilde often represents approximation, especially when used in duplicate, and is sometimes called the "equivalency sign." In regular expressions, the tilde is used as an operator in pattern matching, and in C programming, it is used as a bitwise operator representing a unary negation (i.e., "bitwise ...

What is operators in C?

An operator is a symbol that tells the compiler to perform specific mathematical or logical functions. C language is rich in built-in operators and provides the following types of operators − Arithmetic Operators.

What is the use of in C?

In C/C++, the # sign marks preprocessor directives. If you're not familiar with the preprocessor, it works as part of the compilation process, handling includes, macros, and more.


2 Answers

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

var dict = (IDictionary<string, object>)foo; dict.Remove("bang"); 
like image 149
Jon Avatar answered Sep 25 '22 09:09

Jon


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"); 
like image 29
Jon Skeet Avatar answered Sep 26 '22 09:09

Jon Skeet