我已经添加的列表项基于当前对象(obj)值进行更新。如何使列表不更新或如何复制 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);
}
}