我有一个应用程序可以查询XML (文件),然后将值存储在列表中。我在不同的文件中以相同的方式定义了两个类:
namespace Cnc11Info
{
public class AxisInfo
{
public char V300_Label { get; set; }
public double V300_SlowJog { get; set; }
public double V300_FastJog { get; set; }
}
}
namespace Cnc11Info
{
public class DriveInfo
{
public static double DriveIndex { get; set; }
public static string DriveType { get; set; }
public static double DriveVersion { get; set;
}
}声明列表相同:
public static List<AxisInfo> axesInfo = new List<AxisInfo>();
public static List<DriveInfo> driveInfo = new List<DriveInfo>(); .Add()列出相同的新项目:
MainWindow.axesInfo.Add(new AxisInfo());
MainWindow.driveInfo.Add(new DriveInfo()); 并将数据插入到每个对象的属性中:
MainWindow.axesInfo[index].GetType().GetProperty(propertyName.ToString()).SetValue(MainWindow.axesInfo[index], value);
MainWindow.driveInfo[index].GetType().GetProperty(propertyName.ToString()).SetValue(MainWindow.driveInfo[index], value);其中index = 0-7。在每次迭代中,项目中的每个属性的值都被正确设置,对于列表中的所有8个项目,axesInfoi的每个项目都被正确设置,但对于driveInfo,每个属性都被设置为相同的值。我假设简短的答案是driveInfo中的所有对象都是引用,但是为什么呢?该如何修复呢?


https://stackoverflow.com/questions/41400886
复制相似问题