How do I create a dynamic object that contains another dynamic object in a list?
Let's say I normally have the following non dynamic classes.
Public Class Foo
{
public bool IsSomething { get; set; } = false;
public List<FooItem> FooCollection { get; set; } = new List<FooItem>();
}
Public Class FooItem
{
public string Key { get; set; } = string.Empty;
public string Value { get; set; } = string.Empty;
}
How do I make FooItem dynamic and then create a dynamic Foo with List included.
The following doesnt work (and I know it isn't correct syntax) :
dynamic fooItem = new ExpandoObject();
fooItem.Key = "";
fooItem.Value = "";
dynamic foo = new ExpandoObject();
foo.IsSomething = true;
foo.FooCollection add a dynamic fooItem somehow ?????????
I'm guessing this may be something to do with some required reflection but I'm really struggling to work out how.
What I am actually trying to achieve is to be able to create objects on the fly to return in .NET API's as JSON instead of creating loads of DTO object files simply to then be returned.