对于同一属性是无法定义不同的访问性的。一般情况下,我们定义一个属性会对应到一个私有成员变量,我认为可以通过两个属性对应到同一个私有成员,一个属性实现get,另一个实现set,这样应该可以实现你的需求。For example:private string _x;public string GetX
{
get { return _x; }
}protected string SetX
{
set { _x = value; }
}
{
get { return _x; }
}protected string SetX
{
set { _x = value; }
}
public class B
{
private string _x; public string GetX
{
get { return _x; }
} protected string SetX
{
set { _x = value; }
} }
public class C:B
{
}
//
B b=new B();
C c=new C();
public class B
{
private string _x; public string GetX
{
get { return _x; }
} protected string SetX
{
set { _x = value; }
} public B()
{
}
} public class C:B
{
public C()
{
this.SetX = "Hello World";
}
}2、然后在一个窗口的按钮的单击事件中创建C的实例,并将_x的值赋给该按钮的Text属性。private void button1_Click(object sender, System.EventArgs e)
{
C _c = new C();
this.button1.Text = _c.GetX;
}3、编译运行,结果正确。
如:protected bool ChildControlsCreated {get; set;}看SDK中随便哪一个类,几乎你都能找到 Public properties 和 Protected Poperties 两部分。
可能的方法是:直接使用MSIL编码。
例如,先用C#编写整个Assembly的代码,然后用ildasm.exe导出IL,编辑IL文件,给get/set方法赋予不同的访问属性,然后用ilasm.exe重新汇编成.dll或者exe文件。