Access to internal classes from another project

后端 未结 4 1566
野趣味
野趣味 2020-12-19 00:25

I\'m wondering if it\'s possible to access internal class variables from other project in c#. I know that is impossible in regular use, I explain it below.

I have on

相关标签:
4条回答
  • 2020-12-19 00:40

    You can use the InternalsVisibleTo attribute and provide the name of a specific assembly that can see the internal types in your assembly.

    That being said.. I think you are bordering on over-designing this. If the Settings class belongs only to Assembly A... don't put it in Assembly B... put it in Assembly A.

    0 讨论(0)
  • 2020-12-19 00:44

    That is the point of internal modifier - to not allow this. Either change it to public or use some kind of proxy class to expose the functionality you need to the outer world.

    0 讨论(0)
  • 2020-12-19 00:55

    I think this is what you are looking for:

    http://msdn.microsoft.com/en-us/library/system.runtime.compilerservices.internalsvisibletoattribute.aspx

    0 讨论(0)
  • 2020-12-19 00:57

    Yes it is, using InternalsVisibleTo, but I would only recommend doing this for Unit Test projects.

    Otherwise, you should extract a common interface, make it public and put it in a separate assembly that can be references by both projects.

    You could make all the properties of the interface read-only so that the consumer cannot change them.

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