类的属性是一个对象,怎么才能使这个对象的属性在外部只能读取,不能赋值?
比如
class A
{
public int Value { get; set; }
} class B
{
public A Obj { get; private set; }
public B()
{
this.Obj = new A() { Value = 1 };
}
} class Program
{
static void Main(string[] args)
{
B a = new B();
a.Obj.Value = 2; //怎样才能让这步不被允许?A是dll中的类,不能修改。只能改B和Program。
}
}
比如
class A
{
public int Value { get; set; }
} class B
{
public A Obj { get; private set; }
public B()
{
this.Obj = new A() { Value = 1 };
}
} class Program
{
static void Main(string[] args)
{
B a = new B();
a.Obj.Value = 2; //怎样才能让这步不被允许?A是dll中的类,不能修改。只能改B和Program。
}
}
解决方案 »
- C#如何在panel里画曲线?
- C#如何通过在AutoCAD中拾取一个点,并将点的坐标交给C#变量。
- winform.controls(0)就是一个Textbox,为什么不能直接winform.controls(0).text="value" ?
- 急求FastReport问题
- 初学者:为什么会有这样的错误:只有 assignment、call、increment、decrement 和 new 对象表达式可用作语句
- 请教关于.net framework的问题
- 创建一个队列轮循的公共方法,通过接收参数来控制轮循,做到不管多少个图片,都可以挨个推送,先进先出的原则,求教,队列轮循怎么写,求代码参考下
- 求:c#如何显示象这样的数据?
- 我问个老土的问题行么?
- XPath的表达式怎么使用啊?
- 设计布局与实际布局不同,是怎么回事?
- 扫描RFID问题
class B
{
private A Obj;
public int Value { get; }
public B()
{
this.Obj = new A() { Value = 1 };
}
}
class B
{
private A obj;
public A Obj { get { A tem = new A();复制原来的值; return tem; }; }
public B()
{
this.Obj = new A() { Value = 1 };
}
}
{
private T obj; public Decorator(T value)
{
this.obj = value;
} public object GetProperty(string propertyName)
{
System.Reflection.PropertyInfo property = this.obj.GetType().GetProperty(propertyName);
if (property != null)
return property.GetValue(obj, null);
else
{
return null;
}
}
}
class Program
{
static void Main(string[] args)
{
TestClass a = new TestClass() { Name = "name1" };
Decorator<TestClass> b = new Decorator<TestClass>(a);
var name = b.GetProperty("Name");
}
}