Make your own interface with the property name(s) you want. Then, have your concrete class implement your custom interface.
To keep your code DRY, create a private Dictionary that you delegate all of your work to. You can even have your custom interface be Enumerable (or anything else that IDictionary
implements) by delegating the required methods to your private variable.
Here is an example. You would just need change your code from using IDictionary
to IComplexDataContainer
.
interface IComplexDataContainer<TKey, TValue>
: IEnumerable<KeyValuePair<TKey,TValue>>
{
TValue this[TKey index] { get; set; }
}
class MyComplexDataContainer<TKey, TValue>
: IComplexDataContainer<TKey, TValue>
{
IDictionary<TKey, TValue> hiddenHelper { get; set; }
public MyComplexDataContainer()
{
hiddenHelper = new Dictionary<TKey, TValue>();
}
// delegate all of the work to the hidden dictionary
public TValue this[TKey index]
{
get
{
return hiddenHelper[index];
}
set
{
hiddenHelper[index] = value;
}
}
// Just delegate the IEnumerable interface to your hidden dictionary
// or any other interface you want your class to implement
public IEnumerator<KeyValuePair<TKey, TValue>> GetEnumerator()
{
return hiddenHelper.GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
}
Then you would just use like this:
IComplexDataContainer<string, int> myData = new MyComplexDataContainer<string,int>();
myData["tom"] = 18;
myData["dick"] = 22;
myData["harry"] = myData["tom"];