mercredi 23 octobre 2019

Casting objects from jsonb to different objects

I am developing an ASP.NET Core API that uses events with MediatR to publish them in the system. I am looking to include a scheduling service to the Api where events are stored in a database and pull out later and published using MediatR.

The issue I have is that the objects are stored in the database as jsonb and I need to be able to draw them out and cast them to specific object types. I can store the name of the type in the database and access the type that way. However I can't find a solution to this online.

If there is a better way to do this who thing that would be helpful too, but I'm looking for a solution that looks something like this:

var eventType = Type.GetType("foo");
var eventObject = obj.ToObject<eventType>();

If there is a better way of doing this please let me know!

Thanks.





Aucun commentaire:

Enregistrer un commentaire