Use of Reflection to copy object list


Suppose CreditNoteDetail is a class with 8 properties then following code will create an exact replica of the list of its objects.


private List<CreditNoteDetails> GetTRToCRDetails(List<CreditNoteDetails> objTRFromCRDetails)
{
List<CreditNoteDetails> objCRDetailsTransTo = new List<CreditNoteDetails>();
foreach (CreditNoteDetails db in objTRFromCRDetails)
{
CreditNoteDetails a = new CreditNoteDetails();
Type t=typeof(RSPLGenericERP.BLL.CreditNoteDetails);
foreach (System.Reflection.PropertyInfo f in t.GetProperties())
{
f.SetValue(a,f.GetValue(db,
null),null);
}
objCRDetailsTransTo.Add(a);
}
}