vendredi 1 mai 2020

How do I use a function on individual variables in a collection of objects

I'm looking for a way to apply a function to a collection of objects each with multiple variables of the same time such that I can apply the function to each of the objects in the collection and select which of the variables I want to apply it to. For example: take the class

point{
   int x;
   int y;
   int z;
}

Now let's say that I wish to apply the function "flatten" to a list of these points such that one of it's variables becomes 0.

Where flatten might be something like:

   flatten<variableName>(List<Point> points){
          foreach(var point in points){
             point.<variableName> = 0;
          }
   }

Does anybody know if this is possible with c#, maybe using some kind of reflection like structure? I know it can be done in Javascript and I was hoping to do this in c# as it would prevent a lot of duplicate code (note the real function I want to use this for is far larger and contains multiple sub functions).





Aucun commentaire:

Enregistrer un commentaire