Why does HttpSessionState not implement IDictionary?
HttpSessionState appears to be a typical key -> value collection, so why does it not implement the IDictionary-Interface?
Background: I am trying to output/save the Context of my ASP.NET Website when an error occurs and wanted to do this with a recursive function, that outputs a Collection开发者_如何转开发 and all containing Collections. Because HttpSessionState
only implements ICollection
and IEnumerable
, I am losing the information about the keys if I want to do it in a generic manner (= working with interfaces).
IDictionary
implies that the target collection is capable of quick lookups by key. (As far as I am aware) HttpSessionState
is just a list of items, not a dictionary style structure. As a search of that structure would take linear time there's no reason to treat it as a dictionary. If you need a lot of quick lookups then copy the keys and values into a true dictionary. If you don't need quick lookups, then you'll just need to specialize for that class.
There are more things to an interface than just a list of method prototypes. There are semantics that need to be preserved for an interface too. Quick lookups by key is one such non-explicit assumption for (most) consumers of any IDictionary
.
How about writing your own IDictionary-implementing wrapper that takes an HttpSessionState object in its constructor and behaves as you want? I'm assuming you want to do this so you can swap out other kinds of name-value (IDictionary-implementing) session implementations.
Of course, as Billy points out, this is a great way to dress a poor-performing psuedo-dictionary in dictionary clothes!
Just loop through the Session Keys and reference values like so:
Session[key]
精彩评论