CS0436: Type conflicts with the imported type

后端 未结 5 1897
礼貌的吻别
礼貌的吻别 2020-12-06 09:43

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 withi

相关标签:
5条回答
  • 2020-12-06 09:46

    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.

    0 讨论(0)
  • 2020-12-06 09:59

    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.

    0 讨论(0)
  • 2020-12-06 09:59

    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).

    0 讨论(0)
  • 2020-12-06 10:03

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

    {
      "buildOptions":
      {
        "nowarn":
        [
          "CS0436"
        ]
      }
    }
    
    0 讨论(0)
  • 2020-12-06 10:04

    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.

    0 讨论(0)
提交回复
热议问题