【发布时间】:2021-09-05 02:20:08
【问题描述】:
好的,假设我有一些 C# 代码可以过滤或重组传入的对象(或对象列表),如下所示:
public Customer GetCustomer()
{
var customer = _customerAdapter.GetCustomer();
ModifyCustomer(customer);
return customer;
}
private void ModifyCustomer(Customer c)
{
//some changes to customer object
}
这可行,但我更喜欢功能更强大的方法,即保留新客户并返回新客户(修改后的客户)。
深度克隆是要走的路吗?用 C# 编码时是否推荐?
【问题讨论】:
-
C# 没有内置任何内容,例如,请参阅 stackoverflow.com/questions/6569486/…、stackoverflow.com/questions/129389/… 等。请注意,如果涉及 ORM,这可能会出现问题。
-
这不就是将
void ModifyCustomer(Customer c)改为Customer ModifyCustomer(Customer c)并相信ModifyCustomer的内容做对了吗?