I have seen this, but I am wondering if it would be possible to force the override of a member not marked as virtual or abstract with a derived type (hence that should not break the base object functionalities).
I am referring in particular to overriding:
IDictionary<TKey, TValue> Dictionary { get; }
in
KeyedCollection<TKey, TValue>
with another member type that implements IDictionary, to use another internal storage collection.
Is it possible at all to bypass the limitation?
Note that I would not like to use extensions for this (in my case it would not be helpful anyway).
No- in .net a method must be marked as explicitly virtual or abstract to override it.
EDIT: Given the edits to the question, it sounds like you need to create a custom class that implements
IDictionary<TKey, TValue>(which I think KeyedCollection does), then add another indexer that allows you to get objects byTKey. The indexer would look like this:If you want, you can back virtually all of the IDictionary methods and properties by delegating to a private
KeyedCollection<TKey, TValue>field, and for those you want to implement differently you’d be free to do so. The full class would be too long for an answer, otherwise I’d post it.This goes toward the good development practice Prefer Composition over Inheritance