public void CopyToTest() { var uniqueId = new Guid("21EC2020-3AEA-1069-A2DD-08002B30309D"); UserDto userdto = /* repository.LoadUserFromDb(); */ new UserDto() { Name = "Robert", Role = 2, UniqueId = uniqueId.ToByteArray() }; // create and copy to new instance User user = userdto.CopyTo<User>(); Assert.AreEqual(user.Name, "Robert"); Assert.AreEqual(user.Role, User.UserRole.Admin); Assert.AreEqual(user.UniqueId, uniqueId); // Copy to existing instance user = new User(); userdto.CopyTo(user); Assert.AreEqual(user.Name, "Robert"); Assert.AreEqual(user.Role, User.UserRole.Admin); Assert.AreEqual(user.UniqueId, uniqueId); user = new Dictionary<string, object> { { "UniqueId", "21EC2020-3AEA-1069-A2DD-08002B30309D" }, { "Name", "Fred" }, { "Role", 1 } }.CopyTo<User>(); Assert.AreEqual(user.Name, "Fred"); Assert.AreEqual(user.Role, User.UserRole.Editor); Assert.AreEqual(user.UniqueId, uniqueId); }
public bool SetUser([FromBody] UserDto dto) { CheckUser(dto);//检查用户输入 if (dto.Id != Guid.Empty) { UserDto dbDto = Dbc.Db.GetById <UserDto>(dto.Id); ExHelper.ThrowIfNull(dbDto, "查找更新记录失败."); dbDto.CopyTo(dto, a => a.Id); } else { dto.IsDelete = false; } Dbc.Db.Set(dto); return(true); }