Make a clone of an instanced with all the stored values

ぐ巨炮叔叔 提交于 2020-07-23 09:42:09

问题


I have two classes one called warehouse and one called Warehouselocations. The wareHouse is currently able to create,store and find boxes in warehouselocation.

But now i also need the warehouse to be able to create a cloned version of wareHouseLocation with all the stored information.

   locations = new List<WareHouseLocation>();

This is the list where i store all the information. I want to be able to copy it.

I tried to find the answer my self and even tried some code but so far i had got nothing that works properly.

    public WareHouseLocation DeepCopy() 
    {
        foreach (WareHouseLocation  wareHouseLocation in locations)
            {
                if(wareHouseLocation == null) 
                {
                    return null;
                }
                else 
                {
                   //Need code here
                }
              }
           return null;
        }

The code is currently in the wareHouse class. I be happy for anything that could help me.

    public class WareHouseLocation
    {
        public int FloorID { get; set; }
        public List<I3DStorageObject> storage = new List<I3DStorageObject>();


        public double MaxVolume;
        public double MaxWeight;

        public WareHouseLocation(double height, double width, double depth)
        {
            MaxVolume = height * width * depth;
            MaxWeight = 1000;
        }


        public bool hasAvailableVolumeForObject(I3DStorageObject s)
        {
            double currentVolume = 0;


            foreach (I3DStorageObject obj in storage)
            {
                currentVolume += obj.Volume;
            }

            double available = MaxVolume - currentVolume;


            if (s.Volume <= available)
            {
                return true;
            }
            else
            {
                return false;
            }

        }



    }

Here is the code for the WareHouseLocation.


回答1:


You can achieve it by implementing ICloneable interface:

public class WareHouseLocation : ICloneable
{
    public int FloorID { get; set; }
    public List<I3DStorageObject> storage = new List<I3DStorageObject>();

    public double MaxVolume;
    public double MaxWeight;

    //rest of code

    public object Clone()
    {
        var copy = (WareHouseLocation)MemberwiseClone();
        copy.storage = storage.Select(item => (I3DStorageObject)item.Clone()).ToList();

        return copy;
    }
}

Since you have a List reference type inside WareHouseLocation, you'll need to properly clone this as well by implementing ICloneable for I3DStorageObject as well, because MemberwiseClone copy the reference only, not the referred object itself

public class I3DStorageObject : ICloneable
{
    public double Volume { get; set; }
    public object Clone()
    {
        return MemberwiseClone();
    }
}

You can also have a look at MemberwiseClone for details and examples of deep/shallow copy of objects




回答2:


I think you can use JsonConvert.SerializeObject and JsonConvert.DeserializeObject for copy,

    var json = JsonConvert.SerializeObject(put_your_object_here);
    var copy = JsonConvert.DeserializeObject<your_object_type>(json);


来源:https://stackoverflow.com/questions/59443929/make-a-clone-of-an-instanced-with-all-the-stored-values

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!