更改另一个对象属性时更改了不需要的对象属性c#

unwanted object property changed when changing another object property c#(更改另一个对象属性时更改了不需要的对象属性c#)
本文介绍了更改另一个对象属性时更改了不需要的对象属性c#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

from what i understand in other posts i know that my objects use same place in memory but how to separate these objects? i tried to use new but it didn't work or i didn't used it correctly.

Note that i didnt paste setter and getter here.

class Supermarket
{
    List<Product> _products = new List<Product>{ };
    List<Customer> _customers = new List<Customer>{ };
}

class Customer
{
    List<Product> _purchased= new List<Product>{ };
}
class Product
{
    string _id;
    string _name;
    DateTime _expireDate;
    int _cost;
    int _count;
}

i add one Product in one Method.

Product product = new Product(...);
supermarket.Products.Add(product);

and in another method i want to copy the Product from Supermarket.Products to Supermarket.Customers.Purchased. so i want a copy but i cant get it.

here i want to make Copy but it does not work.

Product product = supermarket.Products[productIndex];
supermarket.Customers[customerIndex].Purchased.Add(product);

now the problem is when i change Product properties in Customer class , Product properties inside Supermarket will change too. for example

supermarket.Customers[customerIndex].Purchased.Last().Count = ...
//now the Product supermarket.Products[productIndex] will change too witch is unwanted

解决方案

The reason why it is not working is because you are doing shallow copy by just adding the pointer of product object into the list, not all properties. So if you change one, another will be affected accordingly.

You can use deep copy following this answer, but this way you have to mark your class as [Serializable]. The simplest way which I think is to use Json serializer:

public static class CloneHelper 
{
    public static T Clone<T>(T source)
    {
        var serialized = JsonConvert.SerializeObject(source);
        return JsonConvert.DeserializeObject<T>(serialized);
    }
}

var copyProduct = CloneHelper.Clone<Product>(product);

Or simply, you can manage by yourself as the below code, then it works:

Product product = supermarket.Products[productIndex];

Product copyProduct = new Product() {
    Id = product.Id,
    Name = product.Name,
    ExpireDate = product.ExpireDate,
    Cost = product.Cost,
    Count = product.Count   
};

supermarket.Customers[customerIndex].Purchased.Add(copyProduct);

这篇关于更改另一个对象属性时更改了不需要的对象属性c#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

DispatcherQueue null when trying to update Ui property in ViewModel(尝试更新ViewModel中的Ui属性时DispatcherQueue为空)
Drawing over all windows on multiple monitors(在多个监视器上绘制所有窗口)
Programmatically show the desktop(以编程方式显示桌面)
c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
LINQ many-to-many relationship, how to write a correct WHERE clause?(LINQ多对多关系,如何写一个正确的WHERE子句?)