2

I have to add new properties in expando object in foreach loop but I am not able to see a way to do it. Here is the example:

var allProperties = new List { "Name", "Email", "Roles" }; allProperties.AddRange(metaDataModel.GetFormattedFolders());

dynamic expando = new ExpandoObject();
foreach (var s in allProperties)
{
    expando.s = string.Empty;
}

It consider 's' as a property instead of considering value of 's' as property name.

Thanks

2
  • What type of object is expando? Commented Apr 14, 2014 at 9:58
  • It is Dynamic: dynamic expando = new ExpandoObject(); Commented Apr 14, 2014 at 10:06

1 Answer 1

7
var expando = new ExpandoObject() as IDictionary<string, Object>;
foreach (var s in allProperties)
{
    expando.Add(s, string.Empty);
}
Sign up to request clarification or add additional context in comments.

3 Comments

above does not work. It says:Cannot apply indexing with [] to an expression of type 'System.Dynamic.ExpandoObject'
Thanks. It works. Not sure why we have to cast it into IDictionary object to acheive this?
ExpandoObject is IDictionary<string,object> underneath but you can't use indexing with[] or adding properties like we do with normal Collections.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.