我知道这听起来有点奇怪,但我会试着解释一下:假设我有一个有很多属性的类,而且它们都是只读的,所以这个类是唯一可以修改其属性的类(它正在侦听一个事件,并使用该事件中包含的信息填充属性)。
但是,我想在一些结构上封装一些属性,以创建一个组织良好的层次结构,所以这些结构的属性也应该是只读的,除了所有者类。例如:
public class A
{
private int a1;
public int A1
{
get{ return a1; }
}
private B structB;
public B StructB
{
get{ return structB; }
}
private method eventListenerMethod(...)
{
a1 = someValue;
structB.B1 = otherValue; //I want that only this class can modify this property!
}
}
public struct B
{
private int b1;
public int B1
{
get{ return b1; } // This property should be modifiable for Class A!!
}
}
我想我不能做到这一点,但有谁知道我怎样才能做到这一点?非常感谢您提前。