Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
314 views
in Technique[技术] by (71.8m points)

c# - 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
        }

    }
}

question from:https://stackoverflow.com/questions/65848237/debugger-shows-wrong-value-of-static-variable-when-set-by-reflection

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...