2

my list item that is already added update base on current object (obj) value. how to make list not update or how to copy obj?

    public static void ReadData<T>(string filename, T obj,string node)
    {
        var xmlDocument = new XmlDocument();
        xmlDocument.Load(filename);
        List<T> objectList = new List<T>();
        XmlNodeList xmlNodeList = xmlDocument.SelectNodes("//"+node);

        for (var i = 0; i < xmlNodeList?.Count; i++)
        {
            var j = 0;
            foreach (var objectProperty in obj.GetType().GetProperties())
            {
                if (objectProperty.CanRead)
                {
                    object value;
                    if (objectProperty.PropertyType == typeof(bool))
                    {
                        value = bool.Parse(xmlNodeList[i].ChildNodes[j].InnerText);
                    }
                    else
                    {
                       value= xmlNodeList[i].ChildNodes[j].InnerText;
                    }
                    objectProperty.SetValue(obj, value);
                    j++;
                }
            }
            objectList.Add(obj);
        }

}

Benni To
  • 47
  • 6
  • 1
    If `T` is a `struct`, then it is already a copy. If `T` is a `class`, then : the only way to do this is via a shallow or deep clone. At that point, you should be searching for "how to clone an object in .NET" – Marc Gravell Jan 26 '18 at 09:42
  • 1
    "how to clone non specific object?" - two people have already given you two different approaches to that; have you tried either? how did it go? – Marc Gravell Jan 26 '18 at 10:14

2 Answers2

3

1.You can implement IConeable

public class ClonableClass : ICloneable
{
   public object Clone()
   {
      return this.MemberwiseClone();
   }
}

Now a and b do not refer to the same object.

var a = new ClonableClass ();
var b = (ClonableClass)a.Clone();

2.The simplest way to deep clone is to just serialize the object then deserialize it.

var objStr= JsonConvert.SerializeObject(obj);
var newObj = JsonConvert.DeserializeObject<T>(objStr);

3.Another way will require brute force coding but you can get every last bit of performance gain. You can just create a new object then assign all properties manually.

Ray Krungkaew
  • 6,652
  • 1
  • 17
  • 28
  • 2
    note that this will work, but implements a shallow clone; it is often important to know whether a clone is shallow or deep. I would also probably recommend fixing the typing, i.e. `public Foo Clone() { /* actual code */ } object ICloneable.Clone() => Clone();` – Marc Gravell Jan 26 '18 at 09:43
  • 1
    For the easy, you can just serialize the object the deserialize it. – Ray Krungkaew Jan 26 '18 at 09:53
1

Serialization and deserialization an object is an option to clone objects.

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

Then clone the desired object;

var obj = new SampleClass
{
    Id = 1,
    Name = ""
};
var clonedObj = obj.Clone();
lucky
  • 12,734
  • 4
  • 24
  • 46