简体   繁体   English

如果将同一个文件包含到两个项目中而忽略CS0436“类型X与导入的类型X冲突”,会发生什么可怕的事情?

[英]What horrible thing happens if I ignore CS0436 “type X conflicts with the imported type X” when the same file is included into two projects?

I have a setup similar to this one - the same .cs file is included into more than one project (technically it resides in on project and a shortcut is added to the other so there's one copy of the file at all times) and then one of those projects is referenced from another one and I get type X conflicts with the imported type X warning. 我有一个与此类似的设置-同一.cs文件包含在多个项目中(从技术上讲,它驻留在项目中,并且快捷方式已添加到另一个项目中,因此始终有一个文件副本),然后一个这些项目中的另一个被引用,我得到type X conflicts with the imported type X警告type X conflicts with the imported type X I know that I can fix this by crafting a class library referenced from both projects , but it's extra work that I'd rather avoid right now. 我知道我可以通过设计两个项目引用的类库来解决此问题,但是我现在想避免这是额外的工作。

Is it safe to ignore the warning in this scenario? 在这种情况下忽略警告是否安全? What bad things can happen? 会发生什么坏事?

The two types will not be interchangeable. 这两种类型将不能互换。 You can have both declared separately, but they will be completely separate types. 可以同时另行申报,但他们将是完全不同的类型。 Type resolution can be really awkward - especially if an extern alias isn't possible. 类型解析确实很尴尬-特别是在无法使用extern别名的情况下。 If they never leave the calling scope, that might be fine. 如果他们从不离开调用范围,那可能很好。 However, in the general case it isn't a good idea and should be avoided. 但是,在一般情况下,这不是一个好主意 ,应避免使用。 Type mismatch at runtime would be very likely, as could be type resolution failures at compile-time. 运行时类型不匹配的可能性很高,编译时类型解析的失败也很可能。

Another option in the future might be assembly neutral types ; 将来的另一个选择是装配中立 ; this doesn't apply unless the compiler framework handles it - and it might be limited to the asp.net mvc stack for all I know, but in theory : 除非由编译器框架处理,否则这将不适用-就我所知,它可能仅限于asp.net mvc堆栈,但从理论上讲

[AssemblyNeutral, AttributeUsage(AttributeTargets.Class
    | AttributeTargets.Interface | AttributeTargets.Struct,
    AllowMultiple = false, Inherited = false)]
internal sealed class AssemblyNeutralAttribute : Attribute { }

then whack [AssemblyNeutral] on your type. 然后敲击您的类型的[AssemblyNeutral]

Found this: 发现了这一点:

You have added a reference to the output of the project. 您已经添加了对项目输出的引用。

In other words, when trying to compile your project, AriaLibrary, to produce AriaLibrary.exe, the compiler imports the assembly AriaLibrary.exe. 换句话说,当尝试编译您的项目AriaLibrary以生成AriaLibrary.exe时,编译器会导入程序集AriaLibrary.exe。 On disk, this file exists from a previous build. 在磁盘上,此文件存在于以前的版本中。

As such, the compiler finds two of that class, one that it tries to compile now, and one from that previous build, and thus you get the warning. 这样,编译器会找到该类中的两个,一个试图立即编译,另一个来自先前的构建,因此您会收到警告。

Since there is no valid reason for having the output a project being imported as a reference to itself, you can safely remove that reference. 由于没有正当理由将输出导入为对自身的引用的项目,因此可以安全地删除该引用。

here: The type 'x' in 'x.cs' conflicts with the imported type 'x' 此处: “ x.cs”中的“ x”类型与导入的“ x”类型冲突

But in answer to your actual question, I'm (guessing) you could have issues from collisions/etc and form run time errors due to this? 但是,为了回答您的实际问题,我(猜测)您可能会因此而发生冲突/等问题并形成运行时错误?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM