1

i have a Shared Project (SharedProject1) with a Class Called myDataInfo.cs and 2 Libraries (Common.csproj and CustomControls.csproj)

, this two libraries uses SharedProject1. now i am using this 2 libraries in a Project Called Test.csproj. now when i want to use myDataInfo.cs i get an error that say

myDataInfo.cs is exist in both Common.csproj and CustomControls.csproj

Chibako
  • 47
  • 4
  • See: [What is the difference between a Shared Project and a Class Library in Visual Studio 2015?](https://stackoverflow.com/questions/30634753/what-is-the-difference-between-a-shared-project-and-a-class-library-in-visual-st) – Olivier Jacot-Descombes Feb 20 '23 at 12:39
  • I also tend to rely on dlls rather than source code so they are all in their own namespace. There will be no repetition. – Jiale Xue - MSFT Mar 09 '23 at 08:44

2 Answers2

2

Shared projects share the source code, not the compiled code. This means that if two projects share the same code they will compile it twice. This results in having two distinct types myDataInfo in Common and a myDataInfo in CustomControls. They happen to have the same name but they are two distinct types. This is because types are identified by their fully qualified type names which consists of an assembly name specification, a namespace specification, and a type name.

Therefore you must share the assemblies (DLLs, EXEs, Packages or Project References), not the source code (Shared Projects).

        ┌────────────────────┐
        │ SharedProject1     │
        │                    │
        │ (should be         │
        │  SharedLibrary1)   │
        │                    │
        └────────────────────┘
          ▲       ▲       ▲
          │       :       │
          │       :       │
          │       :       │
  ┌───────┴──┐    :   ┌───┴─────────────┐
  │ Common   │    :   │  CustomControls │
  └──────────┘    :   └─────────────────┘
           ▲      :    ▲
           │      :    │
       ┌───┴──────┴────┴──┐
       │      Test        │
       └──────────────────┘

In Test the reference to SharedProject1 is only required if Test accesses myDataInfo directly.

See also:

Olivier Jacot-Descombes
  • 104,806
  • 13
  • 138
  • 188
0

Libraries should not use projects. Project depends on libraries. Seems as a structural problem of your application. My suggestion of dependency tree: Test.csproj -> SharedProject1.csproj ->(Common.csproj, CustomControls.csproj)

Titanitus
  • 21
  • 2
  • Test.csproj is a winui app, common and customcontrols is a nuget package. so i need to use a shared class. i dont want to add shared project in test.csproj (because user create this and only install nuget packages) – Chibako Feb 20 '23 at 12:37