<PackageReference Include="BouncyCastle.Cryptography" Version="2.5.0" />

AttributeTable

public class AttributeTable
using System; using System.Collections.Generic; namespace Org.BouncyCastle.Asn1.Cms { public class AttributeTable { private readonly Dictionary<DerObjectIdentifier, object> m_attributes; public Attribute this[DerObjectIdentifier oid] { get { if (!m_attributes.TryGetValue(oid, out object value)) return null; List<Attribute> list = value as List<Attribute>; if (list != null) return list[0]; Attribute attribute = value as Attribute; if (attribute != null) return attribute; throw new InvalidOperationException(); } } public int Count { get { int num = 0; foreach (object value in m_attributes.Values) { List<Attribute> list = value as List<Attribute>; if (list != null) num += list.Count; else { if (!(value is Attribute)) throw new InvalidOperationException(); num++; } } return num; } } public AttributeTable(IDictionary<DerObjectIdentifier, object> attrs) { m_attributes = new Dictionary<DerObjectIdentifier, object>(attrs); } public AttributeTable(Asn1EncodableVector v) { m_attributes = BuildAttributes(v); } public AttributeTable(Asn1Set s) { m_attributes = BuildAttributes(s); } public AttributeTable(Attributes attrs) : this(Asn1Set.GetInstance(attrs.ToAsn1Object())) { } public Asn1EncodableVector GetAll(DerObjectIdentifier oid) { Asn1EncodableVector asn1EncodableVector = new Asn1EncodableVector(); if (m_attributes.TryGetValue(oid, out object value)) { List<Attribute> list = value as List<Attribute>; if (list != null) { foreach (Attribute item in list) { asn1EncodableVector.Add(item); } return asn1EncodableVector; } Attribute attribute = value as Attribute; if (attribute == null) throw new InvalidOperationException(); asn1EncodableVector.Add(attribute); } return asn1EncodableVector; } public IDictionary<DerObjectIdentifier, object> ToDictionary() { return new Dictionary<DerObjectIdentifier, object>(m_attributes); } public Asn1EncodableVector ToAsn1EncodableVector() { Asn1EncodableVector asn1EncodableVector = new Asn1EncodableVector(); foreach (object value in m_attributes.Values) { List<Attribute> list = value as List<Attribute>; if (list != null) { foreach (Attribute item in list) { asn1EncodableVector.Add(item); } } else { Attribute attribute = value as Attribute; if (attribute == null) throw new InvalidOperationException(); asn1EncodableVector.Add(attribute); } } return asn1EncodableVector; } public Attributes ToAttributes() { return new Attributes(ToAsn1EncodableVector()); } public AttributeTable Add(params Attribute[] attributes) { if (attributes == null || attributes.Length < 1) return this; AttributeTable attributeTable = new AttributeTable(m_attributes); foreach (Attribute a in attributes) { AddAttribute(attributeTable.m_attributes, a); } return attributeTable; } public AttributeTable Add(DerObjectIdentifier attrType, Asn1Encodable attrValue) { AttributeTable attributeTable = new AttributeTable(m_attributes); AddAttribute(attributeTable.m_attributes, new Attribute(attrType, new DerSet(attrValue))); return attributeTable; } public AttributeTable Remove(DerObjectIdentifier attrType) { if (!m_attributes.ContainsKey(attrType)) return this; AttributeTable attributeTable = new AttributeTable(m_attributes); attributeTable.m_attributes.Remove(attrType); return attributeTable; } private static void AddAttribute(Dictionary<DerObjectIdentifier, object> attributes, Attribute a) { DerObjectIdentifier attrType = a.AttrType; if (!attributes.TryGetValue(attrType, out object value)) attributes[attrType] = a; else { List<Attribute> list = value as List<Attribute>; if (list != null) list.Add(a); else { Attribute attribute = value as Attribute; if (attribute == null) throw new InvalidOperationException(); List<Attribute> list2 = new List<Attribute>(); list2.Add(attribute); list2.Add(a); attributes[attrType] = list2; } } } private static Dictionary<DerObjectIdentifier, object> BuildAttributes(IEnumerable<Asn1Encodable> e) { Dictionary<DerObjectIdentifier, object> dictionary = new Dictionary<DerObjectIdentifier, object>(); foreach (Asn1Encodable item in e) { AddAttribute(dictionary, Attribute.GetInstance(item)); } return dictionary; } } }