Introduction
Mapping one object to another is a common task in software development. It involves copying the values of properties from one object to another object of a different type or the same type. In C#, there are several ways to map one object to another, such as AutoMapper, ValueInjecter, etc. However, this article will discuss how to map one object to another in C# without using third-party packages.
In this article, we will map one object to another object, which are the same type and different type. Some properties are same, and some are different in case.

For this, we are going to create three different classes. First is Department, which contains DepartmentId and DepartmentName.
public class Department {
public int DepartmentId {
get;
set;
}
public string DepartmentName {
get;
set;
}
}
Second is Employee, which contains various types of properties like integer, string, double, DateTime, and also Department.
public class Employee {
public int EmployeeId {
get;
set;
}
public string FirstName {
get;
set;
}
public string LastName {
get;
set;
}
public string AddressLine1 {
get;
set;
}
public string AddressLine2 {
get;
set;
}
public string City {
get;
set;
}
public string State {
get;
set;
}
public string Zip {
get;
set;
}
public double Salary {
get;
set;
}
public DateTime Dob {
get;
set;
}
public Department Department {
get;
set;
}
}
The third class is NewEmployee which has the same properties as Employee, but here are some properties that are different in case. Also, there are some Nullable properties.
public class NewEmployee {
public int ? EmployeeId {
get;
set;
}
public string firstname {
get;
set;
}
public string lastname {
get;
set;
}
public string addressline1 {
get;
set;
}
public string AddressLine2 {
get;
set;
}
public string City {
get;
set;
}
public string State {
get;
set;
}
public string Zip {
get;
set;
}
public double Salary {
get;
set;
}
public DateTime Dob {
get;
set;
}
public Department Department {
get;
set;
}
}
Next, we will create the object of the Employee class with dummy data, as you can see in the below code snippet.
Next, we will create one generic method called Map, which can take any kind of object as input and return given type of data.
TDATA Map < TDATA > (object oldObject) where TDATA: new() {
// Create a new object of type TDATA
TDATA newObject = new TDATA();
try {
// If the old object is null, just return the new object
if (oldObject == null) return newObject;
// Get the type of the new object and the type of the old object passed in
Type newObjType = typeof(TDATA);
Type oldObjType = oldObject.GetType();
// Get a list of all the properties in the new object
var propertyList = newObjType.GetProperties();
// If the new object has properties
if (propertyList.Length > 0) {
// Loop through each property in the new object
foreach(var newObjProp in propertyList) {
// Get the corresponding property in the old object
var oldProp = oldObjType.GetProperty(newObjProp.Name, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.IgnoreCase | BindingFlags.ExactBinding);
// If there is a corresponding property in the old object and it can be read and the new object's property can be written to
if (oldProp != null && oldProp.CanRead && newObjProp.CanWrite) {
// assign property type of both object to new variables
var oldPropertyType = oldProp.PropertyType;
var newPropertyType = newObjProp.PropertyType;
//check if property is nullable or not. if property is nullable then get it's original data type from generic argument
if (oldPropertyType.IsGenericType && oldPropertyType.GetGenericTypeDefinition() == typeof(Nullable < > )) oldPropertyType = oldPropertyType.GetGenericArguments()[0];
if (newPropertyType.IsGenericType && newPropertyType.GetGenericTypeDefinition() == typeof(Nullable < > )) newPropertyType = newPropertyType.GetGenericArguments()[0];
//check type of both property if match then set value
if (newPropertyType == oldPropertyType) {
// Get the value of the property in the old object
var value = oldProp.GetValue(oldObject);
// Set the value of the property in the new object
newObjProp.SetValue(newObject, value);
}
}
}
}
} catch (Exception ex) {
// If there is an exception, log it
}
// Return the new object
return newObject;
}



Join the conversation! Your thoughts help the community grow.