Generic Extension Method To Map Objects From One Type To Another

In this article, we will learn about Generic Extension method to map objects from one type to another.

Let 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.

  1. Public class Teacher_Interview  
  2. {  
  3.     Public int UID {getset;}  
  4.     Public string Name {getset;}  
  5.     Public string Email {getset;}  
  6.     Public string Subject {getset;}  
  7. }  
  8.   
  9. Public class Teacher_College  
  10. {  
  11.     Public int TID {getset;}  
  12.     Public string Name {getset;}  
  13.     Public string Email {getset;}  
  14. }  

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 Teacher_Interview object.

  1. Teacher_Interview ti = new Teacher_Interview()  
  2. {  
  3.     UID = 101  
  4.     Name = “Faisal Pathan”  
  5.     Email = faisalmpathan@gmail.com  
  6.     Subject = “.NET”  
  7. }  

Now, in a normal case, we copy/use the Teacher_Interview object value in Teacher_College like below.

  1. Teacher_College tc = new Teacher_College()   
  2. {  
  3.     TID = ti.UID,  
  4.     Name = ti.Name,  
  5.     Email = ti.Email  
  6. }  

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 Teacher_Interview class object to the Teacher_College class object.

  1. public static void MatchAndMap<TSource, TDestination>(this TSource source, TDestination destination)  
  2.             where TSource : classnew()  
  3.             where TDestination : classnew()  
  4.         {  
  5.             if (source != null && destination != null)  
  6.             {  
  7.                 List<PropertyInfo> sourceProperties = source.GetType().GetProperties().ToList<PropertyInfo>();  
  8.                 List<PropertyInfo> destinationProperties = destination.GetType().GetProperties().ToList<PropertyInfo>();  
  9.   
  10.                 foreach (PropertyInfo sourceProperty in sourceProperties)  
  11.                 {  
  12.                     PropertyInfo destinationProperty = destinationProperties.Find(item => item.Name == sourceProperty.Name);  
  13.   
  14.                     if (destinationProperty != null)  
  15.                     {  
  16.                         try  
  17.                         {  
  18.                             destinationProperty.SetValue(destination, sourceProperty.GetValue(source, null), null);  
  19.                         }  
  20.                         catch (Exception ex)  
  21.                         {  
  22.   
  23.                         }  
  24.                     }  
  25.                 }  
  26.             }  
  27.   
  28.         }  

This method will work for us and map the properties having the same name. But, we will not call this method directly. We will call from another method which is mentioned below.

  1. public static TDestination MapProperties<TDestination>(this object mapSource)  
  2.             where TDestination : classnew()  
  3.         {  
  4.             var destination = Activator.CreateInstance<TDestination>();  
  5.             MatchAndMap(mapSource, destination);  
  6.   
  7.             return destination;  
  8.         }  

The “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.

  1. Var records = (Teacher_Interview_class_object).MapProperties<Teacher_College_class_object>();   

Full class code

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Reflection;  
  5.   
  6. namespace Faisal_Example.Extention  
  7. {  
  8.     public static class MyExtensions  
  9.     {  
  10.         public static void MatchAndMap<TSource, TDestination>(this TSource source, TDestination destination)  
  11.             where TSource : classnew()  
  12.             where TDestination : classnew()  
  13.         {  
  14.             if (source != null && destination != null)  
  15.             {  
  16.                 List<PropertyInfo> sourceProperties = source.GetType().GetProperties().ToList<PropertyInfo>();  
  17.                 List<PropertyInfo> destinationProperties = destination.GetType().GetProperties().ToList<PropertyInfo>();  
  18.   
  19.                 foreach (PropertyInfo sourceProperty in sourceProperties)  
  20.                 {  
  21.                     PropertyInfo destinationProperty = destinationProperties.Find(item => item.Name == sourceProperty.Name);  
  22.   
  23.                     if (destinationProperty != null)  
  24.                     {  
  25.                         try  
  26.                         {  
  27.                             destinationProperty.SetValue(destination, sourceProperty.GetValue(source, null), null);  
  28.                         }  
  29.                         catch (Exception ex)  
  30.                         {  
  31.   
  32.                         }  
  33.                     }  
  34.                 }  
  35.             }  
  36.   
  37.         }  
  38.   
  39.         public static TDestination MapProperties<TDestination>(this object source)  
  40.             where TDestination : classnew()  
  41.         {  
  42.             var destination = Activator.CreateInstance<TDestination>();  
  43.             MatchAndMap(source, destination);  
  44.   
  45.             return destination;  
  46.         }  
  47.           
  48.     }  
  49. }  

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.