如何映射两个不同对象的属性?

我想知道如何映射两个不同对象的字段并为其分配值。

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; } } 

现在我有一个List对象。 我想将值分配给“manager”类。 任何自动方式来做到这一点。 我可以明确地做并为其分配值。 但我的目标是非常巨大的问题。 我也不想使用任何第三方工具。

注意:它不能为经理提供任何前缀。 它可以是任何东西。 (例如:mgrId可以像mgrCode)

您可以使用reflection,甚至忽略属性框(注意employee.IDmanager.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; } } 

如果您不知道Mgr前缀,则只能通过后缀匹配:

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

还有一个非常狭隘和不切实际的假设 :基于属性顺序的映射(如果您希望2种类型具有在相同序列和数字中定义的属性,唯一的区别是属性名称)。 我不建议任何人在现实生活中使用它,但仍然,这里(只是为了让它更脆弱 :)):

 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)); }); 

使用reflection或AutoMapper 。 我推荐后者,因为如果没有目的,编写新代码是浪费的。

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

如果属性没有惯用的源标识符,则使用AutoMapper的投影 。

 Mapper.CreateMap() .ForMember(dest => dest.MgrCode, opt => opt.MapFrom(src => src.ID)) .ForMember(dest => dest.MgrName, opt => opt.MapFrom(src => src.Name))