473,388 Members | 1,468 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 software developers and data experts.

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 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.
Thanks,
Uttara
Feb 14 '06 #1
1 6056
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#)
------------------------------------------------------------------------
Feb 15 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: andrew queisser | last post by:
I've been trying to dynamically create a class DevT that's derived from a generic base GenBase<T>. It doesn't seem to work. I'm attaching a code sample below that illustrates the problem. ...
7
by: Gene Vital | last post by:
Hi all, I need some help in understanding how to use Generics. I have a class based on a user control that can be put on any Container at runtime, I want to be able to call a method on the...
9
by: sloan | last post by:
I'm not the sharpest knife in the drawer, but not a dummy either. I'm looking for a good book which goes over Generics in great detail. and to have as a reference book on my shelf. Personal...
1
by: Vladimir Shiryaev | last post by:
Hello! Exception handling in generics seems to be a bit inconsistent to me. Imagine, I have "MyOwnException" class derived from "ApplicationException". I also have two classes...
4
by: Cedric Rogers | last post by:
I wasn't sure if I could do this. I believe I am stretching the capability of what generics can do for me but here goes. I have a generic delegate defined as public delegate bool...
5
by: Wiktor Zychla [C# MVP] | last post by:
suppose I have a class class MyContainter { List<something1list1; List<something2list2; ... List<somethingnlistn; othertype1 otherfield1; ...
10
by: Marc Gravell | last post by:
Given a generic method "of T", is there a good way of ensuring that any static ctor on T has executed? Following code demonstrates (TestClass1) that via generics you can use the Type instance long...
7
by: =?Utf-8?B?TXJOb2JvZHk=?= | last post by:
Say I have a class that has a generics List as follows: public List<MyClassmyClassList = new List<MyClass>(); and I want to create another class which tries to add an element of MyClass to...
8
by: Tony Johansson | last post by:
Hello! I have read that in practice, casting proved to be several times faster than using a generic. So the main reason to use generics is not that the performance is better because that's...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.