Search code examples
c#code-reuseinternal

Internal method code reuse


Let's say you have 2 totally separate projects: Project 1 and Project 2. One is a Windows app, and one is a web app.

If both projects need classes A, B and C for their own internal use, what is the best way of promoting code reuse in the classes between the two projects (esp. as the code is updated over time)?

  • Force the classes to be public, break the tidy public interface and make a reference from one project to the other (yuck!)
  • Create a third project for the shared components, and then use them only internally for the main projects (yuck!)
  • "Add" the classes from project 1 to project 2 (going outside the project folder) and accept that project 2 will not have all the classes it needs to build within its project folder (acceptable, but not ideal)
  • Depend on copy-and-paste, source code control cross-references, or some other non-programming stunt.
  • Some other technique that's eluding me at the moment (fingers crossed...)

Note that these are identical, INTERNAL, helper classes that are necessary for both projects.


Solution

  • You could use the InternalsVisibleTo assembly attribute for friend assemblies that allows an assembly to access the types and members that are marked internal in another assembly.