Skip to content

API Proposal: Implement IEnumerable<> on various X509 collection types #49878

@stephentoub

Description

@stephentoub

Background and Motivation

X509Certificate2Collection and related types implement the non-generic enumerable but not generic enumerable. This makes them harder to use with LINQ, and it's very common to see .OfType<X509Whatever>() used with these types, even though all of the elements of the collection are guaranteed to implement that type. This makes consumption both more cumbersome, more confusing, and more expensive. We can't change the signatures of the public GetEnumerator methods on these types, but we can at least implement the interfaces so that the OfType isn't necessary.

Proposed API

public class X509Certificate2Collection : X509CertificateCollection,
+     IEnumerable<X509Certificate2>
{
    ...
}

public sealed class X509Certificate2Enumerator : IEnumerator,
+     IEnumerator<X509Certificate2>
{
    ...
}
    

public sealed class X509ChainElementCollection : ICollection,
+    IEnumerable<X509ChainElement>
{
    ...
}

public sealed class X509ChainElementEnumerator : IEnumerator,
+    IEnumerator<X509ChainElement>
{
    ...
}

public sealed class X509ExtensionCollection : ICollection,
+    IEnumerable<X509Extension>
{
    ...
}

public sealed class X509ExtensionEnumerator : IEnumerator,
+    IEnumerator<X509Extension>
{
    ...
}

All new interface method implementations would be explicit.

(We could also consider implementing ICollection<T> where the existing type implements ICollection.)

Example commit:
stephentoub@d310e84

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions