0

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.

James
  • 4,644
  • 5
  • 37
  • 48
Esdyee
  • 13
  • 2
  • How can cloning a `static` class make sense? Cloning is creating a copy of an object. `static` classes cannot be instantiated so you can't create an object in the first place, so how could you copy one? – jmcilhinney Mar 20 '18 at 01:32
  • 1
    Why are you declaring a class inside a `static` class? – jmcilhinney Mar 20 '18 at 01:34
  • The intent of the creator is unknown. I need one more data in static class. However, the amount of data is high. So I wanted to create a clone efficiently. – Esdyee Mar 20 '18 at 01:47

1 Answers1

0

I don't know why did you want to clone a static class

but if you want to Serialize object you need to use a attribute Serializable to tag your Serialize Class.

[Serializable]
public class MenuData
{
    public string ID { get; set; }
    public string Text { get; set; }
    public string Image { get; set; }
    public Boolean Expanded { get; set; }
}

serialization

EDIT

If you want to clone object, your clone class can implement ICloneable interface.

Here is a sample.

public class MenuList
{
    public List<MenuData> Menus { get; private set; }

    public MenuList()
    {
        Menus = new List<MenuData>()
        {
            new MenuData {
                ID = "1",
                Text = "Service",
                Image = "file_path.png",
                Expanded = false
            },
        };
    }

    public class MenuData : ICloneable
    {
        public string ID { get; set; }
        public string Text { get; set; }
        public string Image { get; set; }
        public Boolean Expanded { get; set; }

        public object Clone()
        {
            return new MenuData()
            {
                ID = this.ID,
                Text = this.Text,
                Expanded = this.Expanded,
                Image = this.Image
            };
        }
    }

    public List<MenuData> CloneMenus()
    {
        return Menus.Select(o => (MenuData)o.Clone()).ToList();
    }
}

You can get Clone List on outside.

MenuList list = new MenuList();
List<MenuData> CloneList = list.CloneMenus();
D-Shih
  • 44,943
  • 6
  • 31
  • 51
  • I want save static class Origin data. By the way, the method changes origin static class data. I wanted to avoid this. My skills are still insufficient. Can you tell me other than this? – Esdyee Mar 20 '18 at 01:55
  • I think you could not clone a static field.That's meaningless.You need to create a object List then clone that make sence. – D-Shih Mar 20 '18 at 02:46
  • thanks. i try again – Esdyee Mar 20 '18 at 02:56
  • @제일일 I add a sample wish can help you – D-Shih Mar 20 '18 at 03:20
  • Wow, Thank you for your help. I realized what I was doing wrong. You have modified your sauce a little to make it work. – Esdyee Mar 20 '18 at 08:27