Re: Generics and Reflection

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



uttara wrote:

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 GenericCollection<Address> mAddresses;

//property
Public GenericCollection<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 GenericCollection<T> class
which is ?DeleteList?. In this list I store the all the objects (of
type T) which I might want to delete later. GenericCollection<T>:
CollectionBase //I have tried inheriting from
System.Collections.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
GenericCollection) 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)memberInfo;

//value is the instance of Employee class
Object propertyValue = property.GetValue(value, null);

Now how do I cast the propertyValue so I get the
GenericCollection<Address> list? If I try this
foreach(IEntity entity in
((Collections.CollectionBase)propertyValue))) {
//Then I cannot get to the DeleteList property.
}

If I try this
foreach(IEntity entity in
((GenericCollection<IEntity>)propertyValue))) {
//Then I get an exception which says it cannot convert a value of
type //GenericCollection<Address> to GenericCollection<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.

First of all, don't overdo generics. Rule of thumb is that you should
be able to write your code without generics just fine and then change
the code so it uses reflection but stop at the point where the code
starts to get more complex than it was without generics.

I wouldn't inherit from CollectionBase, but from Collection<T>. Then
I'd implement an interface IEntityCollection on GenericCollection<T>.
IEntityCollection is an interface which exposes a NONgeneric list of
items to delete (or of type List<IEntity> )

So you can then simply retrieve the items to delete.

So, to use a generic class in code where you don't know the specific
type (thus the value of T), use an interface on the generic class to
access the data. Interfaces are also a way to write generic code, it's
just typed in a different way, and has the lovely side effect that it
makes your code more simpler, not more complex as generics CAN do
sometimes.

FB

--
------------------------------------------------------------------------
Get LLBLGen Pro, productive O/R mapping for .NET: http://www.llblgen.com
My .NET blog: http://weblogs.asp.net/fbouma
Microsoft MVP (C#)
------------------------------------------------------------------------
.



Relevant Pages

  • Generics and Reflection
    ... I have a generic collection which I am using in classes to store a collection of embedded objects. ... Class Employee: IEntity ... Private string mStreetName; ... Say I added an Employee object to this queue. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: data transporting questions
    ... > public class Location implements java.io.Serializable { ... > private int locationId; ... > private String locationName; ... > public class Employee implements java.io.Serializable { ...
    (comp.lang.java.programmer)
  • data transporting questions
    ... private int locationId; ... private String locationName; ... public class Employee implements java.io.Serializable { ... The reason for creating these classes is that for each data request, ...
    (comp.lang.java.programmer)
  • Re: data transporting questions
    ... private int locationId; ... private String locationName; ... The reason for creating these classes is that for each data request, ... Taking Employee example sometimes the data request is only concerned ...
    (comp.lang.java.programmer)
  • Re: Why is class Sample where T : Stream, class ILLEGAL
    ... since it restricts to Employee type only? ... Prior to the arrival of Generics in C# 2 this is exactly what you had to do. ... compiler couldn't give you the compile time type checking you'd like. ... private Node next; ...
    (microsoft.public.dotnet.languages.csharp)