Using Dynamic Types in Protobuf

Learn how to use dynamic data types in Protobuf.

Even strongly typed languages sometimes have a need to work with dynamically assigned data types. Sometimes, we won't know the exact type of an object until a specific scenario occurs. This is why C# gives us the ability to have the object as a variable type or use dynamic.

Protobuf also has this ability. The library of well-known types has a type called Any. This type allows us to assign any message type to a field. It doesn't work with scalar value types, but still delivers the ability to work with dynamically-assigned types. This is what we'll cover in this lesson.

Adding the Any type to a Protobuf definition

We'll make our changes in the following code widget. In its current state, it represents the solution we've built previously.

Get hands-on with 1400+ tech skills courses.