I have a generic collection which I am using in classes to store a
collection of embedded objects.
Class Employee: IEntity
{
Private string mName;
Private int mEmployeeID;
….
Private GenericCollecti on<Address> mAddresses;
//property
Public GenericCollecti on<Address> Addresses
{
Get { return mAddresses; }
Set { mAddresses = value; }
}
}
Class Address: IEntity
{
Private string mStreetName;
Private string mCity;
…..
}
Now I am exposing one more property in the GenericCollecti on<T> class
which is “DeleteList”. In this list I store the all the objects (of type
T) which I might want to delete later.
GenericCollecti on<T>: CollectionBase
//I have tried inheriting from
System.Collecti ons.ObjectModel .Collection<T> also…
{
Private ArrayList mDeleteList;
//property
Public ArrayList DeleteList
{
get { return mDeleteList; }
}
…
}
I have another delete queue (this is different than DeleteList in
GenericCollecti on) that contains all the objects I marked for deletion.
Say I added an Employee object to this queue. Note that this queue can
hold different types of objects (Employee, Department etc).
So I pretty much rely on reflection in order to delete any of the
embedded object collection such as Addresses in Employee. Say “property”
is the property Addresses of the Employee class
PropertyInfo property = (PropertyInfo)m emberInfo;
//value is the instance of Employee class
Object propertyValue = property.GetVal ue(value, null);
Now how do I cast the propertyValue so I get the
GenericCollecti on<Address> list?
If I try this
foreach(IEntity entity in ((Collections.C ollectionBase)p ropertyValue)))
{
//Then I cannot get to the DeleteList property.
}
If I try this
foreach(IEntity entity in ((GenericCollec tion<IEntity>)p ropertyValue)))
{
//Then I get an exception which says it cannot convert a value of type
//GenericCollecti on<Address> to GenericCollecti on<IEntity>
}
Is there any other way I can cast the object easily?
I just want to implement a late delete scheme here which will allow me
to delete all the instances in one place, so I don’t have to take care
of this in each individual class that has embedded objects.
Thanks,
Uttara