Why can reflection access protected/private member of class in C#?

后端 未结 3 658
忘掉有多难
忘掉有多难 2020-11-30 13:59

Why can reflection access protected/private member of class in C#?

Is this not safe for the class, why is reflection given such power? Is this an anti-pattern?

3条回答
  •  醉话见心
    2020-11-30 14:08

    Reflection is absolute necessary for a debugger. Imagine that you are stepping through your program and unable to see values of your private variables. That's probably the reason why reflection works in .NET and Java the way it works, to make debugging really easy.

    If we wouldn't need debuggers, then I can imagine that reflection would be restricted more in the spirit of OOP.

提交回复
热议问题