-2

I have a class which has a property named properties but that property can have different values. Those are dynamic values coming from somewhere and I need to create a request with a structure of Json which has Customer and properties can have different values. I tried the following:

Customer Class

public class Customer{
  public string name {get;set;}
  public dynamic properties {get;set;}
}

These properties can be dynamic. For example - This can be the json that I get

1st Example:

"properties":{
  "name": "Mark",
  "address": {
    "city":"paris"
   }
}

2nd Example:

"properties":{
  "name": "Chris",
  "description":"human",
  "birth":"1990",
  "address": {
    "name":"paris"
   }
}

Whenever I do properties.address.name, it says it can refer to the null reference. I am not sure if dynamic type is correct. How this should be done in C#. If the property can have different values, what is the approach you take?

Liam
  • 27,717
  • 28
  • 128
  • 190
Learn AspNet
  • 1,192
  • 3
  • 34
  • 74
  • 3
    You're not the first person to be dealing with JSON in C#. Is there a particular reason you think you need to build this yourself? – Damien_The_Unbeliever Oct 28 '21 at 14:30
  • Does this answer your question? [Dynamic Object Serialization](https://stackoverflow.com/questions/3055461/dynamic-object-serialization) – Harish Oct 28 '21 at 14:34
  • @Damien_The_Unbeliever What do you mean by myself? We need this to send to a particular product so we need to build it ourselves – Learn AspNet Oct 28 '21 at 14:40
  • Insiped by @Harish idea, you can create custom types with custom properties at runtime with Reflection: https://stackoverflow.com/questions/3862226/how-to-dynamically-create-a-class – Auditive Oct 28 '21 at 14:52
  • 1
    Does this answer your question? [Deserialize JSON into C# dynamic object?](https://stackoverflow.com/questions/3142495/deserialize-json-into-c-sharp-dynamic-object) – Liam Oct 28 '21 at 15:13

1 Answers1

1

Dynamic types are complex but in short, they are evaluated in run time instead of compile time (see: https://www.geeksforgeeks.org/dynamic-type-in-c-sharp/). It doesnt seem like this is what you are dealing with and you can simplify the solution.

It looks like there are many answers to your question (short google came up with: Add properties dynamically at run time in existing class c# and many more) which you can try and implement.

However - maybe it would be easier to use json supporting packages like newtonsoft.json and have a json object as Properties.

Then you would be able to both add and use the json elements (properties):

using Newtonsoft.Json;

public class Customer 
{   
   public string Name {get;set;}   
   public JObject Properties {get;set;} = new JObject();
}

Add with:

customer.Properties["evilProp"] = 666;

And get it with:

var numberFromHell = customer.Properties["evilProp"];
  • This is a good start but it is not working as expected. I am trying the following customer.Properties["evilProp"] = new { name = "Test", address= new { name = "what", last = new { name = "test5" } } }; It says that Cannot convert to Jtoken – Learn AspNet Oct 28 '21 at 15:21
  • I can change it to ToString() but I need this to be in JSON. Can you help there? – Learn AspNet Oct 28 '21 at 15:26
  • `customer.Properties["evilProp"]` doesn't match to what you exampled. `customer.Properties = new { name = "Test", address= new { name = "what" } };` does. And you can't use indexing on dynamic objects. – Auditive Oct 28 '21 at 15:45