Do the access levels and modifiers (private, sealed, etc) serve a security purpose in C#?

后端 未结 5 1041
一生所求
一生所求 2021-01-11 17:08

I\'ve seen that you can manipulate private and internal members using reflection. I\'ve also seen it said that a \'sealed\' class is more secure that one that isn\'t.

5条回答
  •  孤独总比滥情好
    2021-01-11 17:52

    I always try to lock things down to the minimal access required. Like tvanfosson stated, it's really about design more than security.

    For example, I'll make an interface public, and my implementations internal, and then a public factory class/methods to get the implementations. This pretty much forces the consumers to always type it as the interface, and not the implementation.

    That being said, a developer could use reflection to actually instantiate a new instance of an implementation type. There's nothing stopping him/her. However, I can rest knowing that I made it at least somewhat difficult to violate the design.

提交回复
热议问题