Faisal.Map.Object 1.0.2

Generic Extension Method To Map Objects From One Type To Another

There is a newer version of this package available.
See the version list below for details.
Install-Package Faisal.Map.Object -Version 1.0.2
dotnet add package Faisal.Map.Object --version 1.0.2
<PackageReference Include="Faisal.Map.Object" Version="1.0.2" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Faisal.Map.Object --version 1.0.2
The NuGet Team does not provide support for this client. Please contact its maintainers for support.

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.

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

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

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.

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

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

Release Notes

Map method is updated.

Dependencies

This package has no dependencies.

This package is not used by any popular GitHub repositories.

Version History

Version Downloads Last updated
1.0.4 118 2/22/2019
1.0.3 101 2/22/2019
1.0.2 109 2/21/2019