实体 A
namespace MapClass.Models.A { public class UsersModel { public int Id { get; set; } public string UserName { get; set; } public string FullName { get; set; } } }
实体 B
namespace MapClass.Models.B { public class UsersModel { public int Id { get; set; } public string UserName { get; set; } } }
实现
static void Main(string[] args) { Models.A.UsersModel AUser = new Models.A.UsersModel { Id = 1, UserName = "Admin", FullName = "管理员", }; Models.B.UsersModel BUser = Mapping<Models.B.UsersModel, Models.A.UsersModel>(AUser); Console.WriteLine("Hello World!"); } public static R Mapping<R, T>(T model) { R result = Activator.CreateInstance<R>(); foreach (PropertyInfo item in typeof(R).GetProperties()) { PropertyInfo info = typeof(T).GetProperty(item.Name); if (info != null) item.SetValue(result, info.GetValue(model)); } return result; }