Generic list/sublist handling

Posted by user628661 on Stack Overflow See other posts from Stack Overflow or by user628661
Published on 2013-10-24T21:32:14Z Indexed on 2013/10/24 21:54 UTC
Read the original article Hit count: 66

Filed under:
|

Let's say we have a class

class ComplexCls
{
  public int Fld1;
  public string Fld2;
  //could be more fields
}

class Cls
{
  public int SomeField;
}

and then some code

class ComplexClsList: List<ComplexCls>;
ComplexClsList myComplexList;
// fill myComplexList

// same for Cls    
class ClsList : List<Cls>;
ClsList myClsList;

We want to populate myClsList from myComplexList, something like (pseudocode):

foreach Complexitem in myComplexList
{
  Cls ClsItem = new Cls();
  ClsItem.SomeField = ComplexItem.Fld1;
}

The code to do this is easy and will be put in some method in myClsList. However I'd like to design this as generic as possible, for generic ComplexCls. Note that the exact ComplexCls is known at the moment of using this code, only the algorithm shd be generic.
I know it can be done using (direct) reflection but is there other solution? Let me know if the question is not clear enough. (probably isn't). [EDIT] Basically, what I need is this: having myClsList, I need to specify a DataSource (ComplexClsList) and a field from that DataSource (Fld1) that will be used to populate my SomeField

© Stack Overflow or respective owner

Related posts about c#

Related posts about generics