How to run custom code when List.Add or List.Remove is running
-
Hi, Before generics I used something like this to connect childs with parent classes
interface IMyItem
{
CollectionOwner Owner{get;set;}
}
class MyCollection : System.Collections.CollectionBase
{
// ctor
public MyCollection(CollectionOwner owner)
{
_owner = owner;
}
// internal interface
protected override OnAdd(object item)
{
if(item is IMyItem) ((IMyItem)item).Owner = _owner;
}
}this way one was able to add items to a owner class
owner.Items.Add(new Item())
and the owner had the item instance in his list while the item had a reference to his owner, which made a call likeitem.Remove()
possible instead of callingowner.Items.Remove(item)
Now, .net 2.0 introduced generics, way cool. But how do I implement this behaviour with generics? There is no overrideable method instead of using the brute force method withnew
class MyCollection<T> : List<T>
where T : IMyItem{
// ctor
MyCollection(CollectionOwner owner)
{
_owner = owner;
}
new public void Add(T item)
{
item.Owner = _owner;
base.Add(item);
}
}this way if one is casting
MyCollection
back toList<>
my custom add method isn't run. bad design how can I extend generic collection base classes with such logic to do some extra work on each add or remove?myMsg.BehindDaKeys = "Chris Richner";
-
Hi, Before generics I used something like this to connect childs with parent classes
interface IMyItem
{
CollectionOwner Owner{get;set;}
}
class MyCollection : System.Collections.CollectionBase
{
// ctor
public MyCollection(CollectionOwner owner)
{
_owner = owner;
}
// internal interface
protected override OnAdd(object item)
{
if(item is IMyItem) ((IMyItem)item).Owner = _owner;
}
}this way one was able to add items to a owner class
owner.Items.Add(new Item())
and the owner had the item instance in his list while the item had a reference to his owner, which made a call likeitem.Remove()
possible instead of callingowner.Items.Remove(item)
Now, .net 2.0 introduced generics, way cool. But how do I implement this behaviour with generics? There is no overrideable method instead of using the brute force method withnew
class MyCollection<T> : List<T>
where T : IMyItem{
// ctor
MyCollection(CollectionOwner owner)
{
_owner = owner;
}
new public void Add(T item)
{
item.Owner = _owner;
base.Add(item);
}
}this way if one is casting
MyCollection
back toList<>
my custom add method isn't run. bad design how can I extend generic collection base classes with such logic to do some extra work on each add or remove?myMsg.BehindDaKeys = "Chris Richner";
Rather from inheriting from List<T>, have your class implement IList<T>, and keep a List<T> field in your class. Something like this:
public class MyCollection<T> : IList<T>
{
readonly List<T> list = new List<T>();public void Add(T item) { item.Owner = \_owner; list.Add(item); } ....
}
Of course, for you to set the Owner property of the item, there must be a constraint on your T class type. You could accomplish this like this:
interface IOwnable
{
CollectionOwner Owner { get; set; }
}The tell your MyCollection object that only IOwnable objects can be stored in it.
class MyCollection<T> : IList<T>
where T : IOwnable
{
...
}*edit* alternately, as Daniel said, inheriting from the System.Collections.ObjectModel.Collections would require less work. You'll still need the IOwnable constraint if you want to set the owner on the items.
Tech, life, family, faith: Give me a visit. I'm currently blogging about: God-as-Judge, God-as-Forgiver The apostle Paul, modernly speaking: Epistles of Paul Judah Himango
-
Hi, Before generics I used something like this to connect childs with parent classes
interface IMyItem
{
CollectionOwner Owner{get;set;}
}
class MyCollection : System.Collections.CollectionBase
{
// ctor
public MyCollection(CollectionOwner owner)
{
_owner = owner;
}
// internal interface
protected override OnAdd(object item)
{
if(item is IMyItem) ((IMyItem)item).Owner = _owner;
}
}this way one was able to add items to a owner class
owner.Items.Add(new Item())
and the owner had the item instance in his list while the item had a reference to his owner, which made a call likeitem.Remove()
possible instead of callingowner.Items.Remove(item)
Now, .net 2.0 introduced generics, way cool. But how do I implement this behaviour with generics? There is no overrideable method instead of using the brute force method withnew
class MyCollection<T> : List<T>
where T : IMyItem{
// ctor
MyCollection(CollectionOwner owner)
{
_owner = owner;
}
new public void Add(T item)
{
item.Owner = _owner;
base.Add(item);
}
}this way if one is casting
MyCollection
back toList<>
my custom add method isn't run. bad design how can I extend generic collection base classes with such logic to do some extra work on each add or remove?myMsg.BehindDaKeys = "Chris Richner";
Instead of inheriting from List, inherit from System.Collections.ObjectModel.Collection. There you can override ClearItems, RemoveItem and InsertItem, which will be called by all normal Add/Clear/Insert/Remove/RemoveAt methods.
-
Hi, Before generics I used something like this to connect childs with parent classes
interface IMyItem
{
CollectionOwner Owner{get;set;}
}
class MyCollection : System.Collections.CollectionBase
{
// ctor
public MyCollection(CollectionOwner owner)
{
_owner = owner;
}
// internal interface
protected override OnAdd(object item)
{
if(item is IMyItem) ((IMyItem)item).Owner = _owner;
}
}this way one was able to add items to a owner class
owner.Items.Add(new Item())
and the owner had the item instance in his list while the item had a reference to his owner, which made a call likeitem.Remove()
possible instead of callingowner.Items.Remove(item)
Now, .net 2.0 introduced generics, way cool. But how do I implement this behaviour with generics? There is no overrideable method instead of using the brute force method withnew
class MyCollection<T> : List<T>
where T : IMyItem{
// ctor
MyCollection(CollectionOwner owner)
{
_owner = owner;
}
new public void Add(T item)
{
item.Owner = _owner;
base.Add(item);
}
}this way if one is casting
MyCollection
back toList<>
my custom add method isn't run. bad design how can I extend generic collection base classes with such logic to do some extra work on each add or remove?myMsg.BehindDaKeys = "Chris Richner";
Thanks guys, This way round things look like they did in System.Collections.xx namespace. Seems like I just got the wrong base class ;(
myMsg.BehindDaKeys = "Chris Richner";