Dynamic Copy of Objects

kgovindarao523-21772
Posted by kgovindarao523-21772 under C# category on | Points: 40 | Views : 1750
In my project, i faced a scenario like :

I have two classes For Example 'Teacher' and 'Student'
I have some common properties in two classes

class Teacher
{
public int intId { get; set; } //1
public string strName { get; set; } //2
public string strAddress { get; set; } //3
public List<string> Phone { get; set; }
public double dblSalary { get; set; }
public string strPAN { get; set; }

}

class Student
{
public int intId { get; set; } //1
public string strName { get; set; } //2
public string strAddress { get; set; } //3
public string strPhone { get; set; }
public string strClass { get; set; }
public char chrSection { get; set; }
}

now my requirement is : Assign a Student as a Teacher

simply i write a code like this.

private Teacher  fnCopyObject(Student objStdnt)
{
Teacher objTeacher = new Teacher ()
{
intId = objStdnt.intId,
strName = objStdnt.strName,
Phone = new List<string>() { objStdnt.strPhone },
strAddress = objStdnt.strAddress
};
return objTeacher ;
}


But..........
what if there are more than 50 or 100 common properties in both classes?
Have to take each property and assign like this?
I think this is not a good practice.

Hence, I came here with a solution:

This code is generic , and can be applied between any two objects that you want to copy and have common properties.

private D Copy<S, D>(object sourceObj) where D : new()
{
D destinationObj = new D();
if (!ReferenceEquals(sourceObj, null))
{
System.Reflection.PropertyInfo[] dnSourceAttributeLst = typeof(S).GetProperties();
System.Reflection.PropertyInfo[] dnDestAttributeLst = typeof(D).GetProperties();

if (!ReferenceEquals(dnSourceAttributeLst, null))
{
foreach (System.Reflection.PropertyInfo Item in dnSourceAttributeLst)
{
if (!ReferenceEquals(dnDestAttributeLst.FirstOrDefault(x => x.Name == Item.Name), null))
{
if (!ReferenceEquals(dnDestAttributeLst.FirstOrDefault(x => x.Name == Item.Name), null))
{
if (Item.GetValue(sourceObj, null).GetType().Name == dnDestAttributeLst.FirstOrDefault(x => x.Name == Item.Name).PropertyType.Name)
{
if (Item.GetValue(sourceObj, null).GetType().Name == "Int32")
{
dnDestAttributeLst.Where(x => x.Name == Item.Name).FirstOrDefault().SetValue(destinationObj, (int)Item.GetValue(sourceObj, null), null);
}
if (Item.GetValue(sourceObj, null).GetType().Name == "String")
{
dnDestAttributeLst.Where(x => x.Name == Item.Name).FirstOrDefault().SetValue(destinationObj, (string)Item.GetValue(sourceObj, null), null);
}
}
}

}
}
}
}
return destinationObj;

}


We need to call this Function as :

//Initail Object
 Student objStudentSource= new Student()
{
intId = 121,
strName = "TestStudentName",
strAddress = "addr",
strPhone = "8542121315",
strClass = "1st",
chrSection = 'A'
};

//Function Calling
Teacher oTeacher = objProgram.Copy<Student,Teacher>(objStudentSource);

Comments or Responses

Posted by: Prabhukiran345 on: 12/26/2013 Level:Starter | Status: [Member] | Points: 10
Good One Govind..

Login to post response