This is necessary for scripts such as remote access, serialization, materialization, etc. You should not use it blindly, but note that these tools have always been available in any system (in fact, by directly accessing memory). A reflection simply formalizes it and puts controls and checks in a way that you donβt see, because you are apparently using βfull trustβ, so you are already stronger than the system that is protected.
If you try this with partial trust, you will see much more control over the internal state.
Is this an anti-pattern?
Only if your code uses it improperly. For example, consider the following (valid for a WCF contract):
[DataMember] private int foo; public int Foo { get {return foo;} set {foo = value;} }
Is it wrong for WCF to support this? I suspect not ... there are several scenarios in which you want to serialize something that is not part of the public API, without a separate DTO. Similarly, LINQ-to-SQL will be implemented in private members, if you choose.
Marc Gravell Jan 18 2018-10-18T00: 00Z
source share