0

I have:

  1. Json file which contains list of names (MyfunctionA, MyfunctionB, MyfunctionC)

  2. Visual studio project contains multiple functions, each function name is the same as in Json file.

    public static void MyfunctionA() {}
    public static void MyfunctionB() {}
    public static void MyfunctionC() {}
    

I am trying to achieve:

  1. I deserialize Json file to get object.

    dynamic MyJsonObject = JsonConvert.DeserializeObject(json);
    
  2. I use foreach loop

    foreach (var obj in MyJsonObject) {
        // My code here
    }
    

How to call each function in foreach loop?

  • 1
    Please add the example of your json. – Guru Stron May 05 '21 at 12:01
  • 1
    Here you can see how to execute a method by name using Reflection: https://stackoverflow.com/a/3110313/2877982 – Aage May 05 '21 at 12:03
  • Why `dynamic`? Apart from that: _"How to call each function in foreach loop?"_ - Reflection. – Fildor May 05 '21 at 12:03
  • Doesn't make sense that you need to call the deserialize in a loop. You should be getting back one response and deserialize only once. – jdweng May 05 '21 at 12:04
  • @jdweng Huh? As far as I can tell, OP is planning to do exactly that. – Fildor May 05 '21 at 12:05
  • ^^ *Reflection*, _but_ in this case, I'd probably use reflection only to build a `Dictionary` _once_ in the app's lifetime. Then use that as lookup. I guess you might want to restrict the set of functions that shall be callable in this manner, right? – Fildor May 05 '21 at 12:08
  • I deserialize only once to convert Json string to object, then I run foreach loop . My idea is: I do not want to call hundreds of functions one by one, I want to run a loop to write less code. –  May 05 '21 at 12:12
  • 1
    Voted to reopen. Actually, I think there is a better solution than Reflection, here - if it is reasonable for OP. – Fildor May 05 '21 at 12:15
  • So what is the best way to achieve that? I hope I explained it clearly. –  May 05 '21 at 12:21
  • 1
    Have a look at this: https://dotnetfiddle.net/Y1Z8TQ Maybe it's feasable to you. It's just a scribble, so you'll probably want to play around with the idea. – Fildor May 05 '21 at 12:24
  • 1
    This is it, thank you. –  May 05 '21 at 12:29
  • @Fildor : Usually you have to get a response and deserialize each response one at a time. Here the op is getting an array of json objects. When do you ever see more than one json object for each response? – jdweng May 05 '21 at 12:33
  • @jdweng honestly, I have no idea what you're talking about. (No offense) The question doesn't mention "response" _anywhere_. – Fildor May 05 '21 at 12:35
  • @Fildor : Where does the json come from. Normally you get json response from a server. – jdweng May 05 '21 at 13:13
  • @jdweng _"Json **file** which contains list of names"_ ... so I jumped to the conclusion it's from the file system. Maybe I am mistaken, though ... – Fildor May 05 '21 at 13:15
  • @Fildor : What OP is asking is confusing. The list a names is not the json but the classes. I'm not sure where the array is coming from unless the is more than one file or more than one conneciton. – jdweng May 05 '21 at 13:21
  • _"What OP is asking is confusing."_ - agreed. – Fildor May 05 '21 at 13:22
  • Is is super simple :) I will edit my question later with example –  May 05 '21 at 13:27

0 Answers0