2

I'm trying to resolve problem with referencing shared project within couple class libraries.

Inside solution the references between projects are:

  • Class library A -> SharedProject
  • Class library B -> SharedProject
  • Class library C -> SharedProject
  • Class library C -> Class library A
  • Class library C -> Class library B

In shared project I have only one class with logic, that I want to share, so in each class library project I'm using same class from shared project. But after build i get folowing error in Class library C:

The type 'same class' exists in both Class library A and Class library B

Is there a way to make this work, or shoul I replace shared project with classic class library?

Kazenga
  • 476
  • 1
  • 4
  • 15
  • Just make sure both class are under different namespace, problem solved. – Rosdi Kasim Apr 06 '17 at 07:38
  • Each class library has different namespace, the class within shared project has specific namespace, but it is shared among other libraries – Kazenga Apr 06 '17 at 07:41
  • 1
    Of course the same type exists. You compiled it into each DLL. It's in A, B, *and* C. See marked duplicate for details as to how this differs from referencing a common, standalone DLL across multiple projects. The shared project feature is there to facilitate sharing of code in scenarios where a DLL compiled once won't work (e.g. targeting completely different platforms). Personally, I would still just compile the DLL for each platform, but VS offers this feature that incorporates the whole code into the referencing project. It's more like adding existing files as a link, than a reference. – Peter Duniho Apr 06 '17 at 07:50

1 Answers1

4

A 'shared project' is effectively a way of sharing the source code between multiple projects without having to build a DLL (like in a class library). You can think of it as literally copy pasting the source code of SharedProject into Class library A and Class library B.

It should be fairly obvious at this point then that Class library A and Class library B do have the same classes defined in SharedProject, however they both define them, rather than both referencing the same classes.

Your initial intuition is correct, the correct way to do it is to replace it with a class library, which is exactly what it's for. You'll then have a SharedProject.dll that both A and B reference, and then C will reference A.dll, B.dll and SharedProject.dll, and it should all be fine (barring version mismatches).

Mike Roibu
  • 307
  • 2
  • 13