How to compare the properties of two different objects?

I want to know how to match the fields of two different objects and assign values ​​to them.

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 values ​​to the class "manager". Any automatic way to do this. I can do it explicitly and assign values ​​to it. But my object is very huge, this is a problem. I also do not want to use third-party tools.

Note. It cannot have a prefix for the manager. It could be anything. (Example: mgrId may be like mgrCode)

+8
object c # oop
Jul 16 '13 at 11:32
source share
2 answers

You can use reflection for it, even ignoring the property wrapper (note employee.ID vs. manager.MgrId ):

 class Program { static void Main(string[] args) { var employee = new Employee() { ID = 1, Name = "John" }; var manager = new Manager(); foreach (PropertyInfo propertyInfo in typeof(Employee).GetProperties()) { typeof(Manager) .GetProperty("Mgr" + propertyInfo.Name, BindingFlags.IgnoreCase | BindingFlags.Instance | BindingFlags.Public) .SetValue(manager, propertyInfo.GetValue(employee)); } } } 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; } } 

If you do not know the Mgr prefix, you can use only suffixes:

 foreach (PropertyInfo propertyInfo in typeof(Employee).GetProperties()) { typeof(Manager).GetMembers() .OfType<PropertyInfo>() .FirstOrDefault(p => p.Name.EndsWith(propertyInfo.Name, StringComparison.CurrentCultureIgnoreCase)) .SetValue(manager, propertyInfo.GetValue(employee)); } 

And a very narrow and impractical assumption : matching based on the order of the properties (if you expect 2 types to have properties defined in the same sequence and number, the only difference is in the names of the properties). I would not recommend that anyone use it in real life, but still here it is (just make it more fragile :)):

 typeof(Employee) .GetProperties() .Select((p, index) => new { Index = index, PropertyInfo = p }) .ToList() .ForEach(p => { typeof(Manager) .GetProperties() .Skip(p.Index) .FirstOrDefault() .SetValue(manager, p.PropertyInfo.GetValue(employee)); }); 
+14
Jul 16 '13 at 11:45
source share

Use reflection or AutoMapper . I recommend the latter as the new code is wasteful if it has no purpose.

 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; } } Mapper.Initialize(cfg => { cfg.RecognizeDestinationPrefixes("Mgr"); cfg.CreateMap<Employee, Manager>(); }); var manager = Mapper.Map<Employee, Manager>(new Employee { Id = 1, Name = "Fred" }); Console.WriteLine("Id: {0}", manager.MgrId); Console.WriteLine("Name: {0}", manager.MgrName); 

If the properties do not have a source identifier, use the AutoMapper projection .

 Mapper.CreateMap<Employee, Manager>() .ForMember(dest => dest.MgrCode, opt => opt.MapFrom(src => src.ID)) .ForMember(dest => dest.MgrName, opt => opt.MapFrom(src => src.Name)) 
+10
Jul 16 '13 at 11:42 on
source share



All Articles