When you use an RMW implementation which relies on statically generated types (
rmw_opensplice_cpp) this is not possible since it relies on code being generated and compiled for each type.
When you use an RMW implementation which supports dynamic types the mapping happens by looking at the introspection information of the message type and using an API which handles each field individually (
rmw_connext_dynamic_cpp [this one is currently not maintained and will likely not work with the current state of ROS 2 but would need to be updated]). In that case it should be possible to pass arbitrary types to the API as long as a corresponding introspection description of the message is provided with it (which is basically an object representation of the message). We haven’t tried this though so it might be “bumpy” if you go ahead with this.