我在命名空间 Global 中创建了以下类
namespace Global
{
public static class Status
{
public static readonly char Active;
public static readonly char Suspended;
public static readonly char Terminiated;
public static readonly char Deleted;
private static readonly Dictionary<char, string> statusCollection;
public static Dictionary<char, string> StatusCollection
{
get
{
return statusCollection;
}
}
static Status()
{
statusCollection = new Dictionary<char, string>();
statusCollection.Add('A', "Active");
statusCollection.Add('S', "Suspended");
statusCollection.Add('T', "Terminated");
statusCollection.Add('D', "Deleted");
Active = 'A';
Suspended = 'S';
Terminiated = 'T';
Deleted = 'D';
}
}
public class a
{
public void add()
{
//How to make this collection readonly
Status.StatusCollection.Add('N', "asd");
Status.Active = 'M'; //Throws a compile time exception, changes not allowed
}
}
}
奇怪的行为当我尝试Status.Active
在即时窗口中更新时,我期望该值不会改变,但允许更改。这是否意味着我们可以readonly
通过反射或运行时更改变量的值?