Generic Extension Method to Map Objects From One Type to Another
We take a look at some C# code to learn how to map objects to different types using generic extension methods.
Join the DZone community and get the full member experience.
Join For FreeLet me start with one scenario. Just imagine we have an object which has lots of public properties and we need to use only some properties of it for the user profile method. For example, we have a class Teacher_Interview
and another class named Teacher_College
.
Public class Teacher_Interview
{
Public int UID {get; set;}
Public string Name {get; set;}
Public string Email {get; set;}
Public string Subject {get; set;}
}
Public class Teacher_College
{
Public int TID {get; set;}
Public string Name {get; set;}
Public string Email {get; set;}
}
Here, we have written both the classes. As we can see, we have a Teacher_Interview
class that contains its public properties. Now, we need to use the Teacher_College
class object and want the same values from the Teacher_Interview
class object.
For example, we wrote a Teacher_Interview
object.
Teacher_Interview ti = new Teacher_Interview()
{
UID = 101
Name = “Faisal Pathan”
Email = faisalmpathan@gmail.com
Subject = “.NET”
}
Now, in a normal case, we copy/use the Teacher_Interview
object value in Teacher_College
like below.
Teacher_College tc = new Teacher_College()
{
TID = ti.UID,
Name = ti.Name,
Email = ti.Email
}
Just think about what happens if we have 40-50 or more than 50 properties. In this case, we have to write the same line 40 to 50 times which is definitely a time-consuming and boring thing. It is also possible to forget to assign some of the properties which will result in an error.
So, what is the solution for that?
We can use System.reflection
namespace and classes.
Here, I wrote a generic method, namely “MatchAndMap
” which copies the same name properties from the Teacher_Interview
class object to the Teacher_College
class object.
public static void MatchAndMap<TSource, TDestination>(this TSource source, TDestination destination)
where TSource : class, new()
where TDestination : class, new()
{
if (source != null && destination != null)
{
List<PropertyInfo> sourceProperties = source.GetType().GetProperties().ToList<PropertyInfo>();
List<PropertyInfo> destinationProperties = destination.GetType().GetProperties().ToList<PropertyInfo>();
foreach (PropertyInfo sourceProperty in sourceProperties)
{
PropertyInfo destinationProperty = destinationProperties.Find(item => item.Name == sourceProperty.Name);
if (destinationProperty != null)
{
try
{
destinationProperty.SetValue(destination, sourceProperty.GetValue(source, null), null);
}
catch (Exception ex)
{
}
}
}
}
}
This method will work for us and map the properties that have the same name. But, we will not call this method directly. We will call from another method which is mentioned below.
public static TDestination MapProperties<TDestination>(this object mapSource)
where TDestination : class, new()
{
var destination = Activator.CreateInstance<TDestination>();
MatchAndMap(mapSource, destination);
return destination;
}
Activator.CreateInstance
first creates an empty instance for our destination class and passes in the MatchAndMap
method and then returns to the destination.
Now here, we think how to call or use this method. So, here is a way to use this method.
var records = (Teacher_Interview_class_object).MapProperties<Teacher_College_class_object>();
Full class code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
namespace Faisal_Example.Extention
{
public static class MyExtensions
{
public static void MatchAndMap<TSource, TDestination>(this TSource source, TDestination destination)
where TSource : class, new()
where TDestination : class, new()
{
if (source != null && destination != null)
{
List<PropertyInfo> sourceProperties = source.GetType().GetProperties().ToList<PropertyInfo>();
List<PropertyInfo> destinationProperties = destination.GetType().GetProperties().ToList<PropertyInfo>();
foreach (PropertyInfo sourceProperty in sourceProperties)
{
PropertyInfo destinationProperty = destinationProperties.Find(item => item.Name == sourceProperty.Name);
if (destinationProperty != null)
{
try
{
destinationProperty.SetValue(destination, sourceProperty.GetValue(source, null), null);
}
catch (Exception ex)
{
}
}
}
}
}
public static TDestination MapProperties<TDestination>(this object source)
where TDestination : class, new()
{
var destination = Activator.CreateInstance<TDestination>();
MatchAndMap(source, destination);
return destination;
}
}
}
Conclusion
In this article, I explained how we can perform smart work instead of hard work to copy the same class object properties to another class object.
I hope this article will be helpful and useful somewhere in your project. Please give your valuable feedback and share with your friends so they can use this method.
Opinions expressed by DZone contributors are their own.
Comments