lundi 16 octobre 2023

Is it possible to AddModelError for ModelStateDictionary with Reflections?

I have a controller which i doesn't know at compile time. So i need to use reflections to add a modelError. Is that even possible? Or is there another way to do that?

Here is how i thought it might work (but it doesn't).

var modelProperty = controller.GetType().GetProperty("ModelState");
var addErrorMethod = typeof(ModelStateDictionary).GetMethod("AddModelError", new[] { typeof(string), typeof(string) });
addErrorMethod.Invoke(modelProperty, new object [] { "key", "message" });

The error i get: Object does not match target type.





Aucun commentaire:

Enregistrer un commentaire