The only way you could do this would be to have the Books property, which returns a type that has its own suitable index. Here is one possible approach:
public class Indexer<TKey, TValue> { private Func<TKey, TValue> func; public Indexer(Func<TKey, TValue> func) { if (func == null) throw new ArgumentNullException("func"); this.func = func; } public TValue this[TKey key] { get { return func(key); } } } class Library { public Indexer<string, Book> Books { get; private set; } public Indexer<string, DateTime> PublishingDates { get; private set; } public Library() { Books = new Indexer<string, Book>(GetBookByName); PublishingDates = new Indexer<string, DateTime>(GetPublishingDate); } private Book GetBookByName(string bookName) {
But you should seriously consider providing an implementation of IDictionary<,> instead of using this approach, as this will allow you to use other great things, such as enumerating key-value pairs, etc.
cdhowie
source share