Search code examples
c#code-sharing

Share common c# class between 2 projects but allow a project to make changes to it


Let's say i have a Project A that contains common classes that are going to be shared between Project B and Project C.

Is it possible for Project C to implement it's own version of a class from Project A so other classes within Project A will use the Project C version if that is going to be the build target or use the Project B(shared version) version if that is the target.

Having 2 seperate classes would be a solution i think but that is going to be a pain in the ass to make sure i update both versions. An other solution that i can think of would be virtual methods but i have no clue if the shared class dependency's will use the overridden method or the original one.


Solution

  • Don't write a class. Write an interface. Then, by design, a class that depends on the interface can use any implementation of the interface.