I have studied deep copies over the day but I still don't quite get it.
Here's what I want.
static class MyList
List<MenuData> Menu1 = MenuList.Menus.ToList();
List<MenuData> Menu2 = MenuList.Menus.ToList();
I use Menu2 on remove method. However, Menu1 was also deleted. I realize that Menu1 and Menu2 were deleted together because of Swallow Copy.
They also referred to samples of other people, but failed.
static class MenuList
{
public class MenuData
{
public string ID { get; set; }
public string Text { get; set; }
public string Image { get; set; }
public Boolean Expanded { get; set; }
}
public static List<MenuData> Menus = new List<MenuData>()
{
new MenuData {
ID = "1",
Text = "Service",
Image = "file_path.png",
Expanded = false
},
};
public static T Clone<T>(T obj)
{
using (var ms = new MemoryStream())
{
var formatter = new BinaryFormatter();
formatter.Serialize(ms, obj);
ms.Position = 0;
return (T)formatter.Deserialize(ms);
}
}
public static List<MenuData> CopyMenus = MenuList.Clone(MenuList.Menus);
}
ErrMessage : The format 'Models.MenuData' is not marked serializable.
It was asked to reduce further waste of time.