Not completely what are looking for, but here is one way to do in Python. In Python, you can use get_msg_class
from ros2topic.api
to get the message type and then subscribe to the topic. Here is an example:
import rclpy
from rclpy.node import Node
from ros2topic.api import get_msg_class
class TestSubscriber(Node):
def __init__(self):
super().__init__('test_subscriber')
message_type = get_msg_class(self, '/topic_name', include_hidden_topics=True)
print('Message type:', message_type)
self.create_subscription(message_type, '/topic_name', self.on_message_received, 1)
def on_message_received(self, message):
print(message)
def main(args=None):
rclpy.init(args=args)
test_subscriber = TestSubscriber()
rclpy.spin(test_subscriber)
test_subscriber.destroy_node()
rclpy.shutdown()
if __name__ == '__main__':
main()
Test it by running e.g.:
ros2 topic pub /topic_name std_msgs/String '{ data: test_value }'