I am trying to create a custom KeyCollection that will only allow my
domain objects to be added to the Collection. I was hoping I could
achieve the following implementation.
GenericKeyedCollection<int, Person> collection = new
GenericKeyedCollection<int, Person>();
Person person1 = new Person();
person1.ID = 1;
person1.Name = "Jimmy";
Person person2 = new Person();
person2.ID = 2;
person2.Name = "Chris";
collection.Add(person1);
collection.Add(person2);
Assert.AreEqual(2, collection.Count);
Now I thought I understood how to do this but I don't know what to
put in the absratct protected override GetKeyForItem. Can someone
please give me some guidance on what to put here?
Here is my implmentation of the GenericKeyedCollection.
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Text;
namespace Unity.Domain
{
public class GenericKeyedCollection<Key, Type> :
KeyedCollection<Key,
Type>
{
protected override Key GetKeyForItem(Type item)
{
//What do I put here since at runtime
//I don't know what the
type will be?
}
}