Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CS0436: Type conflicts with the imported type

Tags:

c#

warnings

I am including an instance of the same source files in multiple assemblies using the Add As Link option. I specifically need to include an instance of the same source within these assemblies because it is responsible for licence validation which must occur internally to the assembly. Performing licence calls across module boundaries could introduce a security risk.

Some of the projects in my solution that include the code depend on other modules that also include it, resulting in warning CS0436:

"The type [type] in [licence.cs full path] conflicts with the imported type [LicenceClass] in [dependency project also including licence.cs]. Using the type defined in [licence.cs full path]".

I have tried declaring a class alias, but the definitions internal to licence.cs cause the same warning. In the alias, there must be a reference to the duplicated class name which causes the same warning.

I know it is bad practice to duplicate source between assemblies, but it is intentional in this case. I would rather keep a central instance that each assembly links to rather than a dedicated instance with renamed classes to avoid the warnings.

The workaround I have is simply to ignore the warning using a #pragma. Is there a more elegant solution?

like image 888
greenback Avatar asked Feb 19 '13 16:02

greenback


5 Answers

It is worth noting that another way to get such warnings is by simply setting a project in visual studio to reference itself: References -> Solution -> etc etc (how I figured this gem out is left as an exercise to the reader ...)

Visual Studio will happily comply, only to throw a wall of warnings of the type described by OP during build, which is to be expected (upon reflection) since every single class etc is getting defined twice.

like image 82
XDS Avatar answered Oct 23 '22 23:10

XDS


The only time conflicts occur is when two dependent classes include the same class. There are two workarounds:

  1. Disable the warning in classes that cause CS0436:

    #pragma warning disable 0436
    
  2. Have a separate instance of the class, uniquely named in each client project (undesirable from a maintenance point of view).

EDIT: There is also a solution: do what Mark suggests below, and mark duplicate classes internal.

like image 43
greenback Avatar answered Oct 24 '22 00:10

greenback


I had a web application I converted from ASP.NET 3.5 to 4.5 when I moved to VS2015. I started seeing this as a warning, but the solution would still compile. There were no circular references, and cleaning the solution and deleting the bin and obj folders didn't help.

It turns out that VS2015 wasn't happy with some of my classes in the App_Code folder. The classes in here had the same namespace as the rest of the web pages in the parent folder. Once I moved these classes out of the App_Code folder and to the top level of the web application, the warnings went away.

like image 44
Andy S. Avatar answered Oct 23 '22 22:10

Andy S.


In .NET Core you can also disable the warning in project.json:

{
  "buildOptions":
  {
    "nowarn":
    [
      "CS0436"
    ]
  }
}
like image 2
Simon Mattes Avatar answered Oct 23 '22 22:10

Simon Mattes


I had this error but not with 2 different classes!
Each new class where in conflict with itself, so obviously I had that CS0436 Error.

After some struggling found out that it was about Mirror Asset that I was using in my multiplayer Unity project. Mirror somehow was including every new class that I make (and inherit from NetworkBehavior).

My external editor was VSCode (visual studio code, solution might also apply to visual studio).

Solution

in
Edit / Preferences / External tools / "Generate .csproj files for:"
I started testing different settings, and this worked for me:
(Not sure if the exact settings work for all, but not having the right files in project, leads to this error. like my case.)

Click Regenerate project files and restart Unity and VSCode after applying these settings (or the setting that suits your project).

like image 2
Saleh Hosseini Avatar answered Oct 24 '22 00:10

Saleh Hosseini