C# - Internal Properties “readable” in quickwatch but not using reflection?

后端 未结 1 2006
陌清茗
陌清茗 2021-01-20 03:21

I see that the \"Quick Watch\" window has access to all properties, irrespective of access restrictions (internal, protected, private) of a class in a library, even when the

相关标签:
1条回答
  • 2021-01-20 03:58

    Use these flags

    BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance
    

    The GetProperty flag is not required for this operation. You might want to add Static as well.

    Note: You can combine the flags with |, because their integer values are powers of two. See this SO answer.


    NOTE (In response to Lalman's and shanks's concern about security issues of Reflection)

    There is always a way to write bad or dangerous code. It is up to you to do it or not. Reflection is not the regular way of doing things, rather is it meant for the programming of special tools, like o/r-mappers or analysis tools. Reflection is very powerful; however, you should use it wisely.

    0 讨论(0)
提交回复
热议问题