I'm having trouble figuring out how to create array of dynamic generic ExpandoObject elements.
I started with this statement, which works:
/* WORKS */
dynamic expando = new ExpandoObject() as IDictionary<string,object>;
What I want is an array composed of such objects. The syntax I tried is this:
/* DOES NOT WORK */
var expandoArray = new ExpandoObject[] as IDictionary<string, object>
{
new ExpandoObject() as IDictionary<string, object>,
new ExpandoObject() as IDictionary<string, object>
};
The idea is that after expandoArray is created, I could refer to its elements as a dynamic by using a construct like this (see http://weblog.west-wind.com/posts/2012/Feb/01/Dynamic-Types-and-DynamicObject-References-in-C):
dynamic expandoArrayDynamic = expandoArray[x];
So, my two specific questions are: 1. How do I create an array of Expando as IDictionary objects? 2. How do I view the array elements as dynamic objects?
ExpandoObjectimplicitly implementesIDictionary<string,object>and thus the cast isn't necessary.ExpandoObject[] as IDictionary<string, object>is not valid, it should be like:ExpandoObject[] as IEnumerable<IDictionary<string, object>>?askeyword.