vendredi 1 janvier 2021

how to convert string name to property with same name in c#? [duplicate]

    public class MyModel
{
        public int Val1{ get; set; }
        public int Val2 { get; set; }
        public int Val3 { get; set; }
        public int Val4 { get; set; }
        public int Val5 { get; set; }
}

in my controller i get the keywork for fill same property

   [HttpPost]
        public IActionResult Create(string keyword,int result) // keyword ="Val2" i want add value to val2 without switch case.i want use reflection but i dont know
        {
          MyModel model=new model();
          model.Val2=result; // if keyword is Val2 set result to Val2
...
        }

thanks for read my question





Aucun commentaire:

Enregistrer un commentaire