[Mono-bugs] [Bug 430804] Static readonly field optimization doesn' t account for changes via reflection

bugzilla_noreply at novell.com bugzilla_noreply at novell.com
Tue Sep 30 16:22:26 EDT 2008


User lupus at novell.com added comment

--- Comment #5 from Paolo Molaro <lupus at novell.com>  2008-09-30 14:22:26 MDT ---
The spec explicitly says: "initonly marks fields which are constant after they
are initialized" (16.1.2 Field contract attributes). It doesn't talk about any
compile-time behavior (in fact this is the spec about the file format, not
about any compiler).
Let me know in what way "are constant after they are initialized" is not clear.
What you're seeing is simply an optimization that the MS jit apparently isn't
doing for some reason and your code will break when they'll implement it for
your case.
Using reflection to update an initonly field is legitimate (and in fact it
works perfectly well in mono) but it is a workaround, the same way you can use
reflection to change a private field of another type. There is no reason to
reject setting these fields with reflection, think about the private flag, it
is the same, the only difference is that by setting the initonly flag you're
telling the jit that (in the spec words) the field is "constant" once the ctor

I suggest you fix your code, but if you're poking inside an assembly that you
don't own you could run mono with specific optimization flags and live with
either the slowdown or with broken code. For your case you'd need to use:
  mono --O=shared test.exe

Configure bugmail: https://bugzilla.novell.com/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
You are the QA contact for the bug.
You are the assignee for the bug.

More information about the mono-bugs mailing list