CS0436:类型与导入的类型冲突 [英] CS0436: Type conflicts with the imported type

查看:875
本文介绍了CS0436:类型与导入的类型冲突的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用添加为链接选项将相同源文件的实例包含在多个程序集中。我特别需要在这些程序集中包含一个相同源的实例,因为它负责许可证验证,该验证必须在程序内部进行。跨模块边界执行许可证调用可能会带来安全风险。

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.

我的解决方案中的某些项目中包含代码的代码也依赖于包含该代码的其他模块,从而导致警告 CS0436

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


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

"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内部的定义会引起相同的警告。在别名中,必须引用导致相同警告的重复的类名。

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.

我的解决方法是忽略警告使用 #pragma 。有没有更优雅的解决方案?

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

推荐答案

唯一的冲突发生是当两个从属类包含同一类时。有两种解决方法:

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


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

  1. Disable the warning in classes that cause CS0436:

#pragma warning disable 0436


  • 有一个单独的类实例,该实例在每个客户端项目中都是唯一的(从维护的角度来看是不希望的)。

  • Have a separate instance of the class, uniquely named in each client project (undesirable from a maintenance point of view).

    编辑:还有一个解决方案:执行Mark在下面建议的操作,并将重复的类标记为内部

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

    这篇关于CS0436:类型与导入的类型冲突的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆