using Reflection:
using System;
using System.Reflection;
class Test
{
public static void Main()
{
ClassA objClassA = new ClassA();
objClassA.ID = 1;
objClassA.Name = "NameValue";
ClassB objClassB = new ClassB();
CopyProperties(objClassA, objClassB);
int id = objClassB.ID;
string name = objClassB.Name;
}
private static void CopyProperties(object objectA, object objectB)
{
if (objectA == null || objectB == null) throw new ArgumentNullException();
var props = objectA.GetType().GetProperties();
foreach (PropertyInfo pi in props)
{
object value = pi.GetValue(objectA, null);
var prop = objectB.GetType().GetProperty(pi.Name);
if (prop != null)
prop.SetValue(objectB, value, null);
}
}
}
class ClassA
{
public int ID { get; set; }
public string Name { get; set; }
}
class ClassB
{
public int ID { get; set; }
public string Name { get; set; }
}