简体   繁体   中英

How to access “internal” modifier class properties from external assembly in C#

At the outset, let me say that I have seen a sort of solution for the problem, but I found this this is majorly used for testing purpose only.

Could anyone give me a good practice way of calling a internal class's internal static property from the external assembly in C#?

I would appreciate if anyone can give me a small example in C#. I am aware of using "AssemblyVisibleTo" but I was told that it is not a good way to code.

There are three ways to access internal data (whether static or not):

  1. Reflection - this defeats the purpose of keeping the data internal as you now have external code that is directly dependent on the internal implementation
  2. InternalsVisibleTo Attribute - you have been told that this is "not a good way to code", which most would agree with, however you were not given an alternative (hence your question). This has bacically the same drawbacks as reflection (it just makes the client code cleaner).
  3. Add a public-facing API - if this data is useful outside the assembly then perhaps the best solution is to add a public API on top of it! You can still hide the internal mechanisms, which allows you to change the internal guts, so long as the external interface remains intact.

So propose those three alternatives to your client and see which they prefer.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM