0

I have json data whose name is Elements and it has one field Property.Now I want to add those Property inside a class and that class needed to be generated dynamically.

Here is the structure of my json data.

"Elements": [
         {
          "$type": "PCL.DatePicker, PCL",
          "Type": "DatePicker",
          "SelectedDate": "2017-02-23T00:00:00",
          "LabelText": "Date:",
          "Property": "Date1",
          "Visibile": true
        },
        {
          "$type": "PCL.FormEntryField, PCL",
          "Type": "Entry",
          "Text": "",
          "LabelText": "Full Name:",
          "Property": "FullName",
          "Visibile": true
        },
        {
          "$type": "PCL.Picker, PCL",
          "Type": "Picker",
          "DefaultIndex": 0,
          "Values": [ "English", "Hindi", "French", "Chinese", "Arabi" ],
          "LabelText": "Language",
          "Property": "Language",
          "Visibile": true
        },
        {
          "$type": "PCL.FormSwitch, PCL",
          "Type": "Switch",
          "DefaultValue": true,
          "Value": false,
          "Text": null,
          "Property": "Happy",
          "LabelText": "Are You Happy?",
          "Visibile": true
        },
        {
          "$type": "PCL.TimePicker, PCL",
          "Type": "TimePicker",
          "SelectedTime": "12:30:00",
          "LabelText": "Time:",
          "Property": "TIme",
          "Visibile": true
        }
]

Is it possible to do this?

Ankur Gupta
  • 221
  • 3
  • 14
  • So you've got some C# code already running and you want to create a new class at runtime to put into a separate DLL? – Evan Trimboli Mar 01 '18 at 04:59
  • 1
    This seems a little X Y'ish to me – TheGeneral Mar 01 '18 at 04:59
  • How secure is this approach? – Jeroen Heier Mar 01 '18 at 05:04
  • 1
    @MichaelRandall - I agree there. I've definitely generated and used classes at runtime, but never stored them permanently. There was a good reason to do it due to constraints from previous decisions that weren't possible to change, but I still didn't want to. It would be very useful to know what actual problem needs to be solved here because it seems unlikely that a problem that has been sufficiently analysed and researched to arrive at the conclusion that this solution is required would be left with this question presented like this. – moreON Mar 01 '18 at 05:12
  • @moreON @ michaelrandall Well for all we know he wants to cache the pre-generated classes for the application's next run. Caching them inside dll form seems perfectly reasonable to me. _Dynamic proxies; SOAP proxies;_ and indeed _REST proxies_ would benefit –  Mar 01 '18 at 05:22
  • @MickyD however as it stands we have no idea. My spidey senses tell me any answer will be chasing a rabbit down the proverbial hole – TheGeneral Mar 01 '18 at 05:24
  • 1
    @MichaelRandall _Absence Of Evidence Is Not Evidence Of Absence_ –  Mar 01 '18 at 05:27
  • Thank u all for replying.I don't want to save that class permanently.i want that class for temporary purpose.Is it possible to do that in C#.If it is then please provide the sample for help. – Ankur Gupta Mar 01 '18 at 05:28
  • 1
    @MickyD that's a deep statement for 3:31pm AEST, however you get the points – TheGeneral Mar 01 '18 at 05:31
  • @MichaelRandall lol thank-you good sir :) –  Mar 01 '18 at 05:34
  • You can use `System.Reflection.Emit` to generate type. But what's good of it if you can only use properties by string names? Would be different if your type was implemented from interface. Than you just cast it and use it in a normal manner – T.S. Mar 01 '18 at 05:40

1 Answers1

0

You could certainly use a T4 template to read through your JSON file and write out the c# class structure and properties.

That is assuming you will be generating at design time, similar to how entities are generated using edmx files.

Please see the Microsoft resource below for the basics of T4 templates.

msdn.microsoft.com/en-us/library/bb126445.aspx

Jtbs
  • 181
  • 9