0

I want to call multiple method dynamically by respecting a certain order. The idea I had was to create a dynamic list of string that is sorted each time depending of a certain values on my database, the result will look something like this:

["InvokingMethod1","InvokingMethod3","InvokingMethod4"....] // this may vary

This list is extracted from a list of object from my database :

 "affichages": [
    {
      "bloc_affichage": {
        "nom": "affichage_f",
        "active": true,
        "ordre": 1,
        "methodName":"InvokingMethod1"
      }
    },
    {
      "bloc_affichage": {
        "nom": "affichage_sf",
        "active": false,
        "ordre": 2,
        "methodName":"InvokingMethod7"
      }
    },
    {
      "bloc_affichage": {
        "nom": "affichage_se",
        "active": false,
        "ordre": 3,
        "methodName":"InvokingMethod6"
      }
    },
    {
      "bloc_affichage": {
        "nom": "affichage_g",
        "active": true,
        "ordre": 4,
        "methodName":"InvokingMethod3"
      }
    },
    {
      "bloc_affichage": {
        "nom": "affichage_t",
        "active": true,
        "ordre": 5,
        "methodName":"InvokingMethod4"
      }
    }
  ]

by itarating through the list affichages and sorted it by order and active set to true so I can get this sorted List, I want to call each method that is related to each value methodName.

static void InvokingMethodX(Something to_Do)  
{  
    Work1(table);  
    Work2(table);  
    WorkN(table);  
}

In the end I will have this Order:

InvokingMethod1(Something to_Do);
InvokingMethod3(Something to_Do);
InvokingMethod4(Something to_Do);

Is there a way to do properly achieve this ?

Edit 1 :

image.png

Argument type 'void' is not assignable to parameter type 'System.Action'

Taieb
  • 920
  • 3
  • 16
  • 37
  • I've removed the asp.net tag because it's not relevant here. – ProgrammingLlama Oct 13 '22 at 03:04
  • Perhaps keep a `Dictionary>`? – ProgrammingLlama Oct 13 '22 at 03:04
  • @ProgrammingLlama Could you please tell me how ? Even if I try to initialize with my void method i got an error : Argument type 'void' is not assignable to parameter type 'System.Action' – Taieb Oct 13 '22 at 03:10
  • https://stackoverflow.com/questions/4233536/c-sharp-store-functions-in-a-dictionary – ProgrammingLlama Oct 13 '22 at 03:11
  • https://stackoverflow.com/questions/54588667/store-void-functions-in-a-dictionary – ProgrammingLlama Oct 13 '22 at 03:11
  • @ProgrammingLlama Don't know why but I followed the example my void method have an argument maybe this is causing the problem ? I still have Argument type 'void' is not assignable to parameter type 'System.Action' I Added an image of the error – Taieb Oct 13 '22 at 03:17
  • a) You'd want an `Action` not an `Action`, and b) you want to add the method, not a call to the method (i.e. `dict.Add("Do1", AddBlocTableau);`) – ProgrammingLlama Oct 13 '22 at 03:26
  • [![image.png](https://i.postimg.cc/yYt0r7SB/image.png)](https://postimg.cc/mt3cPfw6) Something like this ? error : Expected a method with 'void AddBlocTableau()' signature Thank you for your time – Taieb Oct 13 '22 at 03:31
  • See [previous comment](https://stackoverflow.com/questions/74050016/calling-method-by-respecting-a-certain-order-from-a-list-of-values?noredirect=1#comment130746982_74050016) part A. – ProgrammingLlama Oct 13 '22 at 03:37
  • [Works fine for me](https://rextester.com/NHEO22904). – ProgrammingLlama Oct 13 '22 at 03:39
  • @ProgrammingLlama I think I'm near the solution , there's one thing that is missing, that the method name I'll get it from my sorted list ( it's a string ) I Did something like this : foreach (var bloc in Affi) { actionDict.Add(bloc.BlocAffichage.Nom, bloc.BlocAffichage.MethodName); } the bloc..BlocAffichage.MethodName return this error : Argument type 'string' is not assignable to parameter type 'System.Action' https://i.postimg.cc/NFj2Mj5y/image.png – Taieb Oct 13 '22 at 04:20

0 Answers0