Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting readonly fields (Is this bug?)

While just playing with c#, realized a strange thing.

Here is the code:

class Program
{
    static void Main(string[] args)
    {
        System.Diagnostics.Debug.Write(string.Empty);

        typeof(string)
            .GetField("Empty", BindingFlags.Static | BindingFlags.Public)
            .SetValue(null, "Tolgahan");


        Console.WriteLine(string.Empty);

        // output : Tolgahan
    }
}

Why reflection let us to change readonly fields?


The question is "why the setting via reflection is allowed", not "how to do it", so it is not a duplicate of Can I change a private readonly field in C# using reflection?.

like image 951
Tolgahan Albayrak Avatar asked Aug 30 '12 16:08

Tolgahan Albayrak


2 Answers

In the same way the laws of chemistry say that no reaction can cause an atom of one element to change into an atom of another element, but the laws of physics say it happens all the time. Chemistry is a restricted subset of physics in order to simplify problems into a more solvable manner.

Reflection is the physics in this case, where normal programming is the chemistry. It operates in a simpler mindset. Reflection allows you to circumvent that simpler set of rules, exposing you to new processes, but also new dangers.

like image 61
corsiKa Avatar answered Nov 18 '22 00:11

corsiKa


Because readonly, like private, guards against Murphy, not Machiavelli*.

We use readonly and private and anything else that restricts what we can do, primarily because we have hopefully restricted more incorrect, inconsistent, or just plain stupid things, than we have useful and fruitful things.

But it's still just ones and zeros. If some memory is set to the number "42" and we access it through a readonly field, it wasn't readonly when the object was being created. There's nothing stopping it from being changed except the compiler spotting "hey, first you said you didn't want to change it, now you're trying to change it, what gives? One of those two decisions must be wrong".

Now, there's no promise that reflection will be able to change it, but there's no promise that it won't. Right now, the way reflection works and the way readonly works means that you can change it. At the very least, it would take a lot of work (perhaps with costs affecting us users as well as the team who needs to implement that work) to stop someone who'd presumably gone to the bother of doing this because they thought they had a good reason.

Note that the permissions relating to reflection do stop Machiavelli,

*Strictly, Murphy was talking about precisely how we should design things to stop people accidentally doing something disasterous - readonly is a good example, plugs that can't physically be plugged in the wrong way around a better - and Machiavelli was teaching rather than practising the techniques. It makes for nowhere near as concise a saying though.

like image 45
Jon Hanna Avatar answered Nov 18 '22 00:11

Jon Hanna