Access to type members

Suppose I have 2 types of classes TEmployee (with properties A, B) and TDept (with properties C, D). Then I make a class coming from TList as follows:

TMyCcontainer<T>=class(TList<T>) 

Therefore, I can create instances of TMyCcontainer and populate TEmployee or TDept. Does my TMyCcontainer class have anyway access to properties A, B of TEmployee or properties C, D from TDept?

Of course, the type is generic, so it will not be displayed. And this is a problem that I always encounter with generics - maybe I'm using them incorrectly. I recently found out about the limitations and thought I found out what I did not see.

So, I created 2 interfaces, say IEmployee and IDept, made my 2nd original es classes to be interface objects, and put my constraints in my Tlist, i.e.

 TMyCcontainer<T:IEmployee,IDept>=class(TList<T>) 

Of course, I quickly became disappointed, as this suggests that you should implement BOTH of these interfaces in any type that I introduced into my common TList (TMyContainer), whereas I just want ONE in any particular instance and then another in another instance. I would have to implement both IEmployee and IDept in my TDept class, which is not what I want obv.

Is there a good way to access type members in a generic container? Or I should not use generics to do such things. Ty

+4
source share
1 answer

All the general restrictions that you put on a class must be met by a generic type. It looks like you are really looking for two different types: TMyContainer<TDept> and TMyContainer<TEmployee> . Then you will have access to all the properties of these types individually.

+4
source

All Articles