Reputation: 29501
I'm developing a piece of software that is split up into multiple projects, which you can think of as:
Client and Server both reference Common, and use classes from it. For this reason, the classes in Common are public. However, although I want them to be used by Client and Server, I don't want them to be used by outside code that is using the Client and Server libraries.
Marking the classes as internal is clearly not an option. Is there a way I could make them available to Client and Server, but hide them from the outside world?
Upvotes: 2
Views: 525
Reputation: 10287
You disqualify internal
since you assume your other Assemblies
won't be able to use the internal
Types
, which is not necessarily true.
InternalsVisibleToAttibute
is an Assembly Attribute
which allows you to make the internal
Types
of one projects visible to other selected projects.
This way you can make the common types internal
but use this Attribute
to make them visible to Server and Client.
[assembly:InternalsVisibleTo("Client", "PublicKey=2345456456...")]
[assembly:InternalsVisibleTo("Server", "PublicKey=5345347366...")]
In my opinion, internal
is a generally a little bit of a code smell... It doesn't really protect your code as much as making it harder to be extended later... I would just keep the Types
public
and use the XML documents to assure that the one who uses my Types
will use them as I meant them to be used, but that's just my point of view...
Upvotes: 3
Reputation: 3231
Another option, beside attribute InternalsVisibleTo
, is to create Common
project as Shared Project (available in VS 2015) in this way Client
and Server
will have access to internal classes because shared project will be included in assembly of each of them and not moved to separate assembly as in case with Class Library.
Upvotes: 3
Reputation: 14894
You can use internal
classes and InternalsVisibleToAttribute
in the Common
assembly. Add the attribute to the AssemblyInfo
of the Common
project and specify the assemblies that will have access to its internal
s.
[assembly:InternalsVisibleTo("Client")]
[assembly:InternalsVisibleTo("Server")]
Although I would just make the types public
.
Upvotes: 2