From 86fa35913d309a3293800aab44f320a5fef0647b Mon Sep 17 00:00:00 2001 From: Jon Skeet Date: Tue, 2 Aug 2022 09:38:35 +0100 Subject: [PATCH] Add MapField.MergeFrom to merge maps without failing on duplicate keys This will enable message MergeFrom methods to implement the documented behavior. --- .../src/Google.Protobuf/Collections/MapField.cs | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/csharp/src/Google.Protobuf/Collections/MapField.cs b/csharp/src/Google.Protobuf/Collections/MapField.cs index f0124ee12b..09afb75cdb 100644 --- a/csharp/src/Google.Protobuf/Collections/MapField.cs +++ b/csharp/src/Google.Protobuf/Collections/MapField.cs @@ -237,6 +237,21 @@ namespace Google.Protobuf.Collections } } + /// + /// Adds the specified entries to the map, replacing any existing entries with the same keys. + /// The keys and values are not automatically cloned. + /// + /// This method primarily exists to be called from MergeFrom methods in generated classes for messages. + /// The entries to add to the map. + public void MergeFrom(IDictionary entries) + { + ProtoPreconditions.CheckNotNull(entries, nameof(entries)); + foreach (var pair in entries) + { + this[pair.Key] = pair.Value; + } + } + /// /// Returns an enumerator that iterates through the collection. ///