Then it will not be readonly field. Your only options for initializing real readonly fields are the field initializer and constructor.
However, you can implement some kind of readonly functionality using properties. Make your field as a property. Implement the instance freeze method, which flipped a flag that states that it is no longer allowed to update only readable parts. Ask installers to check this flag.
Keep in mind that you refuse to check compile time to check runtime. The compiler will tell you if you are trying to assign a value for the readonly field from anywhere except the declaration / constructor. Using the code below you will get an exception (or you can ignore the update, none of which is optimal IMO).
EDIT: to avoid repeating validation, you can encapsulate the readonly function in the class.
The revised implementation might look something like this:
class ReadOnlyField<T> { public T Value { get { return _Value; } set { if (Frozen) throw new InvalidOperationException(); _Value = value; } } private T _Value; private bool Frozen; public void Freeze() { Frozen = true; } } class Foo { public readonly ReadOnlyField<int> FakeReadOnly = new ReadOnlyField<int>();
Then your code might do something like
var f = new Foo(); f.FakeReadOnly.Value = 42; f.Freeze(); f.FakeReadOnly.Value = 1337;
The last statement throws an exception.
Brian rasmussen
source share