I want to know how to map fields of two different objects and assign the values to it.
Eample:
public class employee
{
public int ID { get; set; }
public string Name { get; set; }
}
public class manager
{
public int MgrId { get; set; }
public string MgrName { get; set; }
}
Now I have a List object. I want to assign the values to "manager" class. Any automatic way to do that. I can do it explicitly and assigning values to it. But my object is very huge thats the problem. I dont want to use any third party tools too.
Note: It can't have any prefix for manager. It can be anything. (Ex: mgrId can be like mgrCode)
You could use reflection for it, even by ignoring the property casing (notice the
employee.ID
vs.manager.MgrId
):If you don't know the
Mgr
prefix, you could only match by suffixes:And a very narrow and impractical assumption: mapping based on the property order (if you are expecting the 2 types to have properties defined in the same sequence and number, the only difference being the property names). I wouldn't recommend anyone using it in real life, but still, here it is (just to make it more fragile :) ):
Use reflection or AutoMapper. I recommend the latter since writing new code is wasteful if it doesn't have a purpose.
If the properties don't have an idiomatic source identifier then use AutoMapper's projection.