-1

I have a requirement to add a property in existing class as per user configuration, I mean if the user has been configured in the database to add a new property like property Name is 'Name', the datatype is 'string' and value is 'Mishra'. After achieving this, I will save a list of TestClass data in Redis cache.

  class TestClass
    {
        public long ID { get; set; }
    }

I have tried from the link https://msdn.microsoft.com/en-us/library/2sd82fz7.aspx but I want to add an existing class, so can anyone please help me to achieve the same.

I want to render JSON data as per user property (column) configuration for my devex grid.

Data Like-

{
"testClass": [
    {
      "scheduledTime": "2018-07-30T15:30:00",
      "estimatedTime": "2018-07-30T15:30:00",
      "flightNumber": "EY4512",
      "airLine": "assets/images/EY_88x45.jpg",
      "fromVia": "Chengdu,Dallas",
      "partner": "EY2356",
      "status": "Departed",
      "flightColor": null,
      "cityColor": null,
      "publicArrivalPlaceholder1": "CTU,DFW",
      "publicArrivalPlaceholder2": "12_IT",
      "publicArrivalPlaceholder3": "AIR",
      "publicArrivalPlaceholder4": "7/30/2018 3:30:00 PM",
      "publicArrivalPlaceholder5": "7/30/2018 3:30:00 PM",
      "id": "179490",
      "cellRule": "",
      "rowRule": ""
    },
    {
      "scheduledTime": "2018-07-30T17:00:00",
      "estimatedTime": "2018-07-30T14:00:00",
      "flightNumber": "EY1213",
      "airLine": "assets/images/EY_88x45.jpg",
      "fromVia": "Adelaide,Ahmedabad",
      "partner": "EY8562",
      "status": "Departed",
      "flightColor": null,
      "cityColor": null,
      "publicArrivalPlaceholder1": "ADE,AMD",
      "publicArrivalPlaceholder2": "2A_T4",
      "publicArrivalPlaceholder3": "AIR",
      "publicArrivalPlaceholder4": "7/30/2018 5:00:00 PM",
      "publicArrivalPlaceholder5": "7/30/2018 2:00:00 PM",
      "id": "179489",
      "cellRule": "",
      "rowRule": ""
    }]
}

Here scheduledTime, estimatedTime, flightNumber etc are property that i need to resolve at run-time.

radhey_mishra
  • 137
  • 3
  • 18
  • Do you want to add the property at runtime or at compile time? – nvoigt Jul 30 '18 at 10:03
  • What is your main purpose, are you sure a Dictionary will not be enough, or may be dynamic property ? – nzrytmn Jul 30 '18 at 10:11
  • Is there a reason not to use an array with name/value pairs? Or a dictionary? Or even just have the properties default at null and only add that bit of data to the cache if it's not null? You could also just subclass and use a function that determines which type of object to create based on given values. – IAmJersh Jul 30 '18 at 10:22
  • I wan to add property at runtime. my main purpose is render a list type json data for my devex grid. I will get that data from a xml to mapped property from user. – radhey_mishra Jul 30 '18 at 10:40
  • [How to ignore json fields conditionally](https://stackoverflow.com/questions/34304738/how-to-ignoring-fields-and-properties-conditionally-during-serialization-using-j) – Lucas Jul 30 '18 at 10:58
  • How about create the same json pattern dynamically as string and then convert to json with something like this JObject json = JObject.Parse(str); ? you can find more info at https://www.newtonsoft.com/json/help/html/T_Newtonsoft_Json_Linq_JObject.htm – nzrytmn Jul 30 '18 at 15:05
  • I also think this is best way to achieve the same in my case. – radhey_mishra Jul 31 '18 at 01:33

1 Answers1

1

Basically, you cannot change the class definition in C#. But there are some solutions:

  1. Use a custom properties named for example Fields that will be a Dictionary and to add property add it to that dictionary; Or simply instead class use dictionary. Then you when you want to get class properties you just need to iterate over that dictionary.
  2. Create a class in runtime and compile it dynamically; This option is more complex, and you may prefer another options - but it enabled. For more information how to compile class dynamically, see this question. Then you can get a Type object of the class and iterate over its properties (There is the place of TypeBuilder.DefineProperty(); It define a new property to class that created in runtime).
  3. Basically this option is like option 1, but if you're using C# 4+, you can use the keyword dynamic, that can be used for accessing dictionary like an object; For example, create the following class:
dynamic dict = new System.Dynamic.ExpandoObject();
dict.Property = "Value";
dict.Num = 123;
Console.WriteLine(dict.Property);
Console.WriteLine(dict.NonExistProperty); // Will throw an exception

For more information about dynamic object in C#, see Microsoft Docs.

Edit:

Note that dynamic objects handled by DLR (Dynamic Language Runtime) and not by CLR (Common Language Runtime), and so code that use them is less effecient.
Chayim Friedman
  • 47,971
  • 5
  • 48
  • 77