mardi 11 avril 2023

Is there a generic way to recursively check a deserialized object for null fields?

I receive a number of different messages that I deserialize into very dissimilar objects

Most of these objects have non-nullable fields, but across all the message types (From GraphQL, Azure service- and storage-bus, to CosmosSB and probably more that I am forgetting) some of them are happy to deserialize into objects with null fields anyway.

As such, I could very much use a component that will let me do

T objectFromMessage = GetObject(message);
FieldAsserter.AssertAllFieldsSet<T>(objectFromMessage); //Throws if any non-nullable fields are null

Do you know if any such field validation method already exists, either by default or somewhere in Nuget? Alternatively, if I have to write it myself, can you give me any clues re. where to start looking?

What did you try and what were you expecting?

Nothing really, at this point - A cursory search of NuGet and a few thoughts about reflection is as far as I got before thinking "Maybe SO has an answer."

There are a couple of similar questions on SO already, but they appear to work for single objects - the answers are of type "write an extension method to your class that does it" but I would like a generic method that'll work for anything - I have too many message types.





Aucun commentaire:

Enregistrer un commentaire