CollectionBase bug?

M

m. pollack

Hi all,

I've been using the CollectionBase class to implement a
strongly-typed collection, but I have noticed that the
RemoveAt method does not seem to call the "On" hook
methods (OnRemove, OnRemoveComplete). The Add method works
fine, though! I need these to work -- I am exposing the
collection to the user through a PropertyGrid control, and
the user can add/remove collection elements with the
(apparently undocumented) Object Collection Editor that
the PropertyGrid invokes, and I need to do some work when
that happens...

Is this a bug? I saw a reference to the problem here:

http://lists.ximian.com/archives/public/mono-list/2001-
November/001978.html

Any thoughts?

Thanks, MP
 
E

Edward Diener

m. pollack said:
Hi all,

I've been using the CollectionBase class to implement a
strongly-typed collection, but I have noticed that the
RemoveAt method does not seem to call the "On" hook
methods (OnRemove, OnRemoveComplete). The Add method works
fine, though! I need these to work -- I am exposing the
collection to the user through a PropertyGrid control, and
the user can add/remove collection elements with the
(apparently undocumented) Object Collection Editor that
the PropertyGrid invokes, and I need to do some work when
that happens...

Can you not override the RemoveAt method ? If the RemoveAt method calls the
IList::Remove method, your implementation of that will catch the remove.
 
J

JD

Are you positive? I looked at the IL for the CollectionBase::RemoveAt and
the two statements do exist in the method call. Look below:

..method public hidebysig newslot virtual final
instance void RemoveAt(int32 index) cil managed
{
// Code size 83 (0x53)
.maxstack 3
.locals (object V_0)
IL_0000: ldarg.1
IL_0001: ldc.i4.0
IL_0002: blt.s IL_0012
IL_0004: ldarg.1
IL_0005: ldarg.0
IL_0006: call instance class System.Collections.ArrayList
System.Collections.CollectionBase::get_InnerList()
IL_000b: callvirt instance int32
System.Collections.ArrayList::get_Count()
IL_0010: blt.s IL_0022
IL_0012: ldstr "ArgumentOutOfRange_Index"
IL_0017: call string System.Environment::GetResourceString(string)
IL_001c: newobj instance void
System.ArgumentOutOfRangeException::.ctor(string)
IL_0021: throw
IL_0022: ldarg.0
IL_0023: call instance class System.Collections.ArrayList
System.Collections.CollectionBase::get_InnerList()
IL_0028: ldarg.1
IL_0029: callvirt instance object
System.Collections.ArrayList::get_Item(int32)
IL_002e: stloc.0
IL_002f: ldarg.0
IL_0030: ldloc.0
IL_0031: callvirt instance void
System.Collections.CollectionBase::OnValidate(object)
IL_0036: ldarg.0
IL_0037: ldarg.1
IL_0038: ldloc.0
IL_0039: callvirt instance void
System.Collections.CollectionBase::OnRemove(int32,

object)
IL_003e: ldarg.0
IL_003f: call instance class System.Collections.ArrayList
System.Collections.CollectionBase::get_InnerList()
IL_0044: ldarg.1
IL_0045: callvirt instance void
System.Collections.ArrayList::RemoveAt(int32)
IL_004a: ldarg.0
IL_004b: ldarg.1
IL_004c: ldloc.0
IL_004d: callvirt instance void
System.Collections.CollectionBase::OnRemoveComplete(int32,

object)
IL_0052: ret
} // end of method CollectionBase::RemoveAt
 
M

Mick Doherty

The link you gave showed a problem where the On* methods were not called
when referencing the InnerList.

From the MSDN documention:
\\\
The On* methods are invoked only on the instance returned by the List
property, but not on the instance returned by the InnerList property.
///

OnRemove and OnRemoveComplete do work at Runtime, but do not appear to work
at DesignTime.
What does appear to happen though, is the Collection Editor clears and
creates a new List whenever you add or remove items from the List. I've not
got an answer as yet, but I'll also need it shortly.
 
G

Guest

Thanks, Ed - this did not solve the problem, though. It
seems that the Collection Editor bypasses these methods
altogether when removing items...

MP
 
G

Guest

Thanks, JD, for looking at that.

I think the problem might be that the Collection Editor
destroys the old list and creates a new one when removing
items, so that the RemoveAt method never gets called.

MP
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top