Reputation: 27295
Does ExpandoObject have any convenience factory methods? Something like, I don't know,
dynamic disney = new ExpandoObject("First", "Donald", "Last", "Duck");
Upvotes: 1
Views: 86
Reputation: 149
Nope, there isn't, but you can write it in minutes. :) Here you go:
C#
class Program
{
static void Main(string[] args)
{
dynamic ex = ExpandoFactory.Create("First", "Donald", "Last", "Duck");
Console.WriteLine(ex.First);
Console.WriteLine(ex.Last);
}
}
static class ExpandoFactory
{
internal static ExpandoObject Create(params string[] items)
{
//safety checks omitted for brevity
IDictionary<string, object> result = new ExpandoObject();
for (int i = 0; i < items.Length; i+=2)
{
result[items[i]] = items[i + 1];
}
return result as ExpandoObject;
}
}
Of course, you should check the cardinality of the array beforehand. I hope this helps.
Upvotes: 5