日期:2014-05-18  浏览次数:20896 次

如何修改List<Point3D>对象pp中的第二个元素由pp[1].Y=2变为pp[1].Y的值为5
对于
  List<Point3D> pp = new List<Point3D >();
  pp.Add(new Point3D(1, 1, 1));
  pp.Add(new Point3D(1, 2, 1));
  pp.Add(new Point3D(1, 3, 1));

现在pp[1].Y=2,如何修改pp[1]使得pp[1].Y的值为5

下列直接赋值语句不行:
pp[1].Y=5 错误


------解决方案--------------------
Point3D 这个是只读的当然不能赋值、
pp.RemoveAt(1);
pp.Add(new Point3D(1, 5, 1));
------解决方案--------------------
探讨
also:

var p=pp[1];
p.Y=5;

------解决方案--------------------
C# code
List<Point3D> pp = new List<Point3D >();
pp.Add(new Point3D(1, 1, 1));
pp.Add(new Point3D(1, 2, 1));
pp.Add(new Point3D(1, 3, 1));
Point3D[] pp1 = pp.GetType().GetField("_items", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance).GetValue(pp) as Point3D[];
pp1[1].Y=5

------解决方案--------------------
查看了MSDN才发现Point3D 是个struct

C# code

[SerializableAttribute]
[TypeConverterAttribute(typeof(Point3DConverter))]
public struct Point3D : IFormattable