samedi 28 mars 2015

Web API 2 - how to get dynamic value using string instead of key?

I have a Web API controller looking similar to this:



public void Post([FromBody]dynamic postData)
{
foreach (var row in postData)
{
var email = row.email.Value; // ok
var eventType = row.event.Value; // cannot use because "event" is reserved by .NET, C# or whatever
}
}


I'm getting a JSON from external system which contains this "event" property (outside of my control), that I'm unable to retrieve. I've tried dozens of workaround, but none of them seemed to work in this scenario. Hopefully there is some easy way of retrieving it.


The best bet I had was using reflection like:



row.GetType().GetProperty("event").GetValue(row, null);


But didn't work as I had expected. Is there something else I can try?


Thank you for any advice.





Aucun commentaire:

Enregistrer un commentaire