Add TrySetValue and TryGetValue#322
Conversation
|
There is probably another method I could add too: Actually, I'm just going to add that now to this PR. |
RehanSaeed
left a comment
There was a problem hiding this comment.
Seems reasonable. Is there a specific reason you need this feature? I can't think of any obvious use cases.
The primary motivation for me is that it makes it easier to create Schema objects at runtime - especially when the properties are provided from unreliable 3rd party data (for example, parsing Microdata from HTML into Schema objects). Besides these methods, there are a couple of other changes I'd like to make for other methods of reading/writing/converting schema objects easier too. For example, moving some of the string-conversion logic in |
I should clarify with this - I was originally going to include some of that in this PR but some of the changes would overlap with the changes for System.Text.Json support and would make merging a bit of a pain. |
Introduces two new methods
TrySetValueandTryGetValuewhich allow dynamic access to get and set values on a schema object. Both methods are generated via our source generator rather than doing something weird with reflection.While
TrySetValueis straight forward,TryGetValuerequires a generic parameter to identify which piece of data you want. This is required to have a typed-value be the result of the call. If you specify a type that is invalid for the property, the method fails gracefully.The methods are also case insensitive.
Open to any feedback or suggestions you have @RehanSaeed 🙂