仍然可以从 C# 中的另一个 class 访问私有变量
private variable is still accessible from another class in C#
我有一个 .cs 文件,如下所示
namespace TarkovMapper.ClassObjects
{
class PointCloud_Object
{
public void AddPoint(PointEntry_Object point)
{
PointLayer pointLayer = LoadPointLayer(path);
pointLayer.Points[point.Location_x,point.Location_y]++;
}
private PointLayer LoadPointLayer(string path)
{
if (!File.Exists(path)) return new PointLayer(this.Width, this.Height);
Stream s = File.OpenRead(path);
BinaryFormatter b = new BinaryFormatter();
PointLayer returnObject = (PointLayer) b.Deserialize(s);
s.Close();
return returnObject;
}
}
[Serializable]
class PointLayer
{
public PointLayer(int width, int height)
{
this.Points = new int[width, height];
}
public int[,] Points { get; private set; } // <- private set!!!
public int Maximum { get; private set; }
}
}
我的问题是关于 class PointLayer 中的变量“Points”。
尽管我有修改器 private set;
PointCloudObject 中的以下行没有问题 pointLayer.Points[point.Location_x,point.Location_y]++;
.
这是为什么?
修饰符指的是 Points 数组,而不是数组的各个元素。
PointCloud_Object class 无法将新数组分配给 PointLayer.Points 变量,但它可以操作单个数组元素。
我有一个 .cs 文件,如下所示
namespace TarkovMapper.ClassObjects
{
class PointCloud_Object
{
public void AddPoint(PointEntry_Object point)
{
PointLayer pointLayer = LoadPointLayer(path);
pointLayer.Points[point.Location_x,point.Location_y]++;
}
private PointLayer LoadPointLayer(string path)
{
if (!File.Exists(path)) return new PointLayer(this.Width, this.Height);
Stream s = File.OpenRead(path);
BinaryFormatter b = new BinaryFormatter();
PointLayer returnObject = (PointLayer) b.Deserialize(s);
s.Close();
return returnObject;
}
}
[Serializable]
class PointLayer
{
public PointLayer(int width, int height)
{
this.Points = new int[width, height];
}
public int[,] Points { get; private set; } // <- private set!!!
public int Maximum { get; private set; }
}
}
我的问题是关于 class PointLayer 中的变量“Points”。
尽管我有修改器 private set;
PointCloudObject 中的以下行没有问题 pointLayer.Points[point.Location_x,point.Location_y]++;
.
这是为什么?
修饰符指的是 Points 数组,而不是数组的各个元素。 PointCloud_Object class 无法将新数组分配给 PointLayer.Points 变量,但它可以操作单个数组元素。