vendredi 22 janvier 2021

Debugger shows wrong value of static variable when set by reflection

When I set a static bool field to true using reflection and I use it in an if-statement, the debugger shows true but the logic sees false.

Does anyone know whats happening here?


static class TestClass
{
    public static void SetValueUsingReflection()
    {
        var aField = typeof(TestClass).GetField(nameof(MyBool));
        aField.SetValue(null, true);
    }
    public static readonly bool MyBool;
}

class Program
{
    static void Main(string[] args)
    {
        TestClass.SetValueUsingReflection();

        if (TestClass.MyBool) // Debugger says "True"
        {
        }
        else
        {
            // Program steps in here
        }

    }
}





Aucun commentaire:

Enregistrer un commentaire