简体   繁体   中英

Mark only one internal class as visible to other assemblies

I know the existance of the InternalsVisibleTo Attribute, which make all the internal classes visible to external assemblies.

In my case I have an Utilities project where all the classes should be visible to only one specific assembly . All but one class, which contains Extension methods that should be visible to all the other assemblies in the solution and not to the external projects.

Which is the best way to achieve this result?

Well, one way to achieve this, if you are using an IDE capable of this, is to share the file with the class with the Extension methods. This way if you mark it as internal, and all other classes as public, then you can have access to the extension methods, but it is not public. Also, when you edit the extension method class, this will be reflected in all of your assemblies in your solution. In Visual studio, the way to achieve this is to right click your project and select Add -> Add Existing item:

在此处输入图片说明

And then find your file and select Add as link:

在此处输入图片说明

Please note that friend assemblies should be used mostly for debugging and testing purposes. Using them as a feature is a code smell.

Split them up. Put the common class in its own assembly.

There is no way to specify specific classes as being "internals visible to".

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