C# CS0436:类型与导入的类型冲突

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14962066/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 13:33:46  来源:igfitidea点击:

CS0436: Type conflicts with the imported type

c#warnings

提问by pdm2011

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:

我的解决方案中包含代码的一些项目依赖于也包含它的其他模块,从而导致警告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]".

“[licence.cs 完整路径] 中的类型 [type] 与 [依赖项目也包括 licence.cs] 中导入的类型 [LicenceClass] 冲突。使用 [licence.cs 完整路径] 中定义的类型”。

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.

我曾尝试声明类别名,但 licence.cs 内部的定义会导致相同的警告。在别名中,必须引用导致相同警告的重复类名。

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?

我的解决方法是使用#pragma. 有没有更优雅的解决方案?

采纳答案by pdm2011

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

  1. 在导致 CS0436 的类中禁用警告:

    #pragma warning disable 0436
    
  2. 有一个单独的类实例,在每个客户端项目中唯一命名(从维护的角度来看是不可取的)。

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

编辑:还有一个解决方案:按照马克在下面的建议进行操作,并标记重复的类internal

回答by XDS

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 中设置一个项目来引用自身:参考 -> 解决方案 -> 等等(我如何想出这个 gem 留给读者作为练习...... )

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.

Visual Studio 很乐意遵守,只是在构建期间抛出 OP 所描述类型的警告墙,这是可以预料的(根据反射),因为每个类等都被定义了两次。

回答by Andy S.

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 binand objfolders didn't help.

当我迁移到 VS2015 时,我有一个从 ASP.NET 3.5 转换为 4.5 的 Web 应用程序。我开始将此视为警告,但解决方案仍会编译。没有循环引用,清理解决方案并删除binobj文件夹也无济于事。

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.

事实证明,VS2015 对 App_Code 文件夹中的一些类并不满意。此处的类与父文件夹中的其余网页具有相同的命名空间。一旦我将这些类移出 App_Code 文件夹并移至 Web 应用程序的顶层,警告就消失了。

回答by Simon Mattes

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

在 .NET Core 中,您还可以禁用 project.json 中的警告:

{
  "buildOptions":
  {
    "nowarn":
    [
      "CS0436"
    ]
  }
}