How can I get all the properties of an instance of a class and assign the values of those properties to the values of properties with matching names and types from an instance of another class. As shown below:-
public ClassA(ClassB objClassB)
{
//So rather than do this
//this.Property1 = objClassB.Property1;
//this.Property2 = objClassB.Property2;
//this.Property3 = objClassB.Property3;
//...
//this.Propertyn = objClassB.Propertyn;
// As I have many properties to assign
// I want to do something like this
foreach (var prop in this.GetType().GetProperties())
{
// Get the property in objClassB matching prop (in name and property type).
// Then assign the value of that property to the value of prop in ClassA,
// so that the result will be same as the code block commented out above
}
}
Please I do not want to use AutoMapper or any other third-party library.
Thanks
Aucun commentaire:
Enregistrer un commentaire