entb
entb

Reputation: 41

Convert WebHeaderCollection to NameValueCollection in .Net Core

Before of dotNetCore (.Net Core), WebHeaderCollection was inherit from NameValueCollection, but not now.

And I need to convert WebHeaderCollection (HttpWebResponse.Headers) to the type property NameValueCollection.

Has anyone ever had to do this?

Upvotes: 2

Views: 2824

Answers (1)

VMAtm
VMAtm

Reputation: 28355

You can convert the collection iterating the keys, for example:

var nvCollection = new NameValueCollection(whCollection.Count)
foreach (var key in whCollection)
{
    nvCollection.Add(key, whCollection[key]);
}
return nvCollection;

Unfortunately, current object hierarchy doesn't allow more generic way to achieve this by LINQ or constructor initilization.

Upvotes: 3

Related Questions