I picked up on a post on SADeveloper where people are getting an exception thrown that says:
“An unhandled exception of type 'System.ArgumentException' occurred in system.dll
Additional information: Source array was not long enough. Check srcIndex and length, and the array's lower bounds.
at System.Array.Copy(Array sourceArray, Int32 sourceIndex, Array destinationArray, Int32 destinationIndex, Int32 length) “
when using the Remove
method in the PropertyDescriptorCollection class. I decided to try figure it out and I must admit there are some funny issues here, this is what I have uncovered so far, will have to dig a bit deeper on this one:
Firstly here is a simple console app as a test case:
using System;
using System.ComponentModel;
namespace colltest
{
/// <summary>
/// Summary description for Class1.
/// </summary>
class Class1
{
private string mtest;
public string test
{
get
{
return mtest;
}
set
{
mtest = value;
}
}
private string mtest2;
public string test2
{
get
{
return mtest2;
}
set
{
mtest2 = value;
}
}
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
Class1 myObj = new Class1();
PropertyDescriptorCollection PDC = new PropertyDescriptorCollection(null);
PDC.Add(TypeDescriptor.GetProperties(myObj)["test"]);
PDC.Add(TypeDescriptor.GetProperties(myObj)["test2"]);
PropertyDescriptor pd1 = TypeDescriptor.GetProperties(myObj)["test"];
PDC.Remove(pd1);
}
}
}
Ok back to the problem at hand:
Now if you try remove the Descriptor "test" then you will experience the above mentioned error.
Put two Descriptors into your collection and remove the second one “test2“, it works, however this is interesting, break and look at the collection before and after the remove:(look at the "count" and "properties.Length" properties)
before: both have a value of 2
after: count is 1 and properites.Length is still 2.
I think there is some footwork in the way this collection operates. and yip the removing of position 0 is a problem from what I can see.
It looks like the Remove only flags the "relevant -1" Descriptor, but how I'm not sure yet. I will do some more digging.