我想知道,因为可以使用反射完成很多事情,我可以在构造函数完成执行后更改私有只读字段吗?
(注意:只是好奇心)
public class Foo
{
private readonly int bar;
public Foo(int num)
{
bar = num;
}
public int GetBar()
{
return bar;
}
}
Foo foo = new Foo(123);
Console.WriteLine(foo.GetBar()); // display 123
// reflection code here...
Console.WriteLine(foo.GetBar()); // display 456
Run Code Online (Sandbox Code Playgroud) 我有我的程序使用...各种常数string的,int的,double的,等...什么是存储它们的最好方法?我不认为我想要一个Enum,因为数据不是所有相同的类型,我想手动设置每个值.我应该把它们全部存放在一个空的课堂上吗?或者,还有更好的方法?