ConstructionBase.jl getproperties does this with a generated function:
Flatten.jl can choose a specific subset of fields based on types or FieldMetadata.jl tags. However, it doesn’t return a named tuple as it gets a tuple from nested objects that may have the same field names. So it returns a Tuple. But the code is kinda hard to understand…
https://github.com/rafaqz/Flatten.jl/blob/master/src/Flatten.jl#L105-L133