0

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.

Lana North
  • 69
  • 2
  • 8
  • As a rule-of-thumb, if you ever find yourself using `dynamic` in C# then _you're probably doing something wrong_: The whole point of using a statically-typed language is for type-safety, but using `dynamic` means you lose all of the advantages while gaining zero benefit... so _why_ are you using `dynamic` here? – Dai Mar 31 '22 at 00:41
  • 1
    "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." - **don't use `dynamic` for that**, instead you should use your JSON library's JSON types (`JToken`/`JObject` in Newtonsoft.Json, or `JsonDocument` in `System.Text.Json`) - you can build runtime-defined JSON documents that way without needing any reflection or subverting the type-system. – Dai Mar 31 '22 at 00:42
  • @Dai I roughly get what your saying here. Please could you give me a simple example of what you mean as to JsonDocument in System.Text.Json. – Lana North Mar 31 '22 at 00:45
  • Please be more specific - I can't just write your code for you. What problems are you having with [`JsonDocument`](https://learn.microsoft.com/en-us/dotnet/api/system.text.json.jsondocument?view=net-6.0) or [`JObject`](https://www.newtonsoft.com/json/help/html/t_newtonsoft_json_linq_jobject.htm) are you having trouble with? – Dai Mar 31 '22 at 00:59
  • @Dai As to your first answer I pretty much agree. I never use then (probably why I don’t understand how they work). But as to API’s I don’t want to expose what is going on internally so to speak so I always return specific information. I actually think some type of (on the fly thing whatever it ends up being) works well here. Creating loads of DTO files so to speak I think is a bit of a waste of time where type safety isn’t really relevant. – Lana North Mar 31 '22 at 00:59
  • @Dai "What specific parts about JsonDocument or JObject are you having trouble understanding?" I am just reading up now but sometimes a simple example just saves time. – Lana North Mar 31 '22 at 01:00
  • Cheers, I will have a read through now. – Lana North Mar 31 '22 at 01:05
  • I’m sorry but now after investigation most of what you are saying is contradictory and wrong. You pretty much aspire to “don't use dynamic” and yet the first link you give me https://stackoverflow.com/questions/18246716/creating-json-on-the-fly-with-jobject the accepted answer is using dynamic. – Lana North Apr 01 '22 at 23:19
  • And as to “no-one writes DTOs by hand, that's what T4 is for”. I had not heard of T4 and for good reason. After asking most of my colleagues (many who have been developing in .NET since its inception) *their* reasons where this: – Lana North Apr 01 '22 at 23:19
  • Writing DTO’s by hand doesn’t take long. Writing DTO’s is pretty much self-documenting and makes the code much clearer in the future than trying to work out what some tool creates. No third-party library is required (this one breaks a lot from *their* experience.) Why introduce one when it takes seconds to code then in concrete. – Lana North Apr 01 '22 at 23:19
  • And the last link you posted: https://stackoverflow.com/questions/58302522/how-to-add-property-in-existing-json-using-system-text-json-library Is not a clear description about how to use JObject, the clue is in the title “How to add a property in existing JSON”. – Lana North Apr 01 '22 at 23:20
  • This should not have been closed as you have far from answered the question. – Lana North Apr 01 '22 at 23:20

0 Answers0