Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Referencing shared project in several projects of solution

I am trying to fix warning

Warning CS0436: The type 'Class1' in '...\SharedProject1\SharedProject1\Class1.cs' conflicts with the imported type 'Class1' in 'ClassLibrary1, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'. Using the type defined in '...\SharedProject1\SharedProject1\Class1.cs'. WpfApplication1 ...\SharedProject1\WpfApplication1\MainWindow.xaml.cs

Repro:

  • create solution with 3 projects:

SharedProject1 (add new class to it)

namespace SharedProject1
{
    public class Class1() { }
}

ClassLibrary1

namespace ClassLibrary1
{
    public class Class1 { }
}

WpfApplication1 (add this to MainWindow constructor)

public MainWindow()
{
    InitializeComponent();
    var a = new SharedProject1.Class1();
    var b = new ClassLibrary1.Class1();
}
  • reference SharedProject1 in both ClassLibrary1 and WpfApplication1;

  • build, you will get a warning.

Question: how to fix the warning?

like image 615
Sinatr Avatar asked Apr 14 '16 09:04

Sinatr


1 Answers

Change the dependency schema from:

Shared -> Class
Shared -> Application

to:

Shared -> Class -> Application

That is: remove from Application a direct reference to Shared.

The first schema results in same class built into 2 dlls. That's what causes the conflict. In the second schema the shared library is built into Class dll and thus is also accesible to Application.

The first schema would be ok, if Class and Application were independent of each other.

All of this is because a shared project does not generate a library. So one must think about making it appear somewhere in a library. Usually only in one place. That usually means, that each shared library should be referenced only once.

like image 191
Jarekczek Avatar answered Oct 15 '22 10:10

Jarekczek