internal static T[]? ReadOptionalSetOf <T>(this AsnReader reader, int?context, Func <AsnReader, T> readElement)
        {
            // ToArray to ensure immediate materialization. If done lazily, it will break integrity validation, as the data wont be consumed.
            return(reader.ReadOptional(context, (r, tag) => r.ReadSetOf(tag), set => ReadCore(set).ToArray()));

            IEnumerable <T> ReadCore(AsnReader set)
            {
                while (set.HasData)
                {
                    yield return(readElement(set));
                }
                set.ThrowIfNotEmpty();
            }
        }
 internal static T?ReadOptionalSequence <T>(this AsnReader reader, int?context, Func <AsnReader, T> read) => reader.ReadOptional(context, (r, tag) => r.ReadSequence(tag), read);