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

后端 未结 4 1725
暗喜
暗喜 2020-12-15 04:42

Say I have this object:

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

How would I remove foo.bang

相关标签:
4条回答
  • 2020-12-15 05:16

    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");
    
    0 讨论(0)
  • 2020-12-15 05:17

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

    var dict = (IDictionary<string, object>)foo;
    dict.Remove("bang");
    
    0 讨论(0)
  • 2020-12-15 05:24

    MSDN Example:

    dynamic employee = new ExpandoObject();
    employee.Name = "John Smith";
    ((IDictionary<String, Object>)employee).Remove("Name");
    
    0 讨论(0)
  • 2020-12-15 05:24

    You can cast it as an IDictionary<string,object>, and then use the explicit Remove method.

    IDictionary<string,object> temp = foo;
    temp.Remove("bang");
    
    0 讨论(0)
提交回复
热议问题