diff --git a/src/csharp/Grpc.Core.Api/Metadata.cs b/src/csharp/Grpc.Core.Api/Metadata.cs index f7f0ae1c93a..a80345a6ba3 100644 --- a/src/csharp/Grpc.Core.Api/Metadata.cs +++ b/src/csharp/Grpc.Core.Api/Metadata.cs @@ -75,7 +75,67 @@ namespace Grpc.Core return this; } - // TODO: add support for access by key + /// + /// Gets the last metadata entry with the specified key. + /// + public Entry Get(string key) + { + for (int i = entries.Count - 1; i >= 0; i--) + { + if (entries[i].Key == key) + { + return entries[i]; + } + } + + return null; + } + + /// + /// Gets all metadata entries with the specified key. + /// + public IEnumerable GetAll(string key) + { + for (int i = 0; i < entries.Count; i++) + { + if (entries[i].Key == key) + { + yield return entries[i]; + } + } + } + + /// + /// Gets the last metadata entry string value with the specified key. + /// + public string GetValue(string key) + { + return Get(key)?.Value; + } + + /// + /// Gets the last metadata entry bytes value with the specified key. + /// + public byte[] GetValueBytes(string key) + { + return Get(key)?.ValueBytes; + } + + /// + /// Adds a new ASCII-valued metadata entry. See Metadata.Entry constructor for params. + /// + public void Add(string key, string value) + { + Add(new Entry(key, value)); + } + + /// + /// Adds a new binary-valued metadata entry. See Metadata.Entry constructor for params. + /// + public void Add(string key, byte[] valueBytes) + { + Add(new Entry(key, valueBytes)); + } #region IList members @@ -135,22 +195,6 @@ namespace Grpc.Core entries.Add(item); } - /// - /// Adds a new ASCII-valued metadata entry. See Metadata.Entry constructor for params. - /// - public void Add(string key, string value) - { - Add(new Entry(key, value)); - } - - /// - /// Adds a new binary-valued metadata entry. See Metadata.Entry constructor for params. - /// - public void Add(string key, byte[] valueBytes) - { - Add(new Entry(key, valueBytes)); - } - /// /// ///