Search code examples
c#visual-studioshared-librariesshared-project

How to Use 2 libraries (contain a Shared Class) in a Project?


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


Solution

  • 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: