简体   繁体   English

警告“Y.cs 中的 X 类型与 Z.dll 中导入的 X 类型冲突”

[英]Warning “The type X in Y.cs conflicts with the imported type X in Z.dll”

The main.cs of my project returns the following warning:我的项目的main.cs返回以下警告:

Warning 1 The type 'Extensions.MessageDetails' in 'PATH\Extensions.cs' conflicts with the imported type 'Extensions.MessageDetails' in 'path\lib.dll'.警告 1 'PATH\Extensions.cs' 中的类型 'Extensions.MessageDetails' 与 'path\lib.dll' 中的导入类型 'Extensions.MessageDetails' 冲突。 Using the type defined in 'path\Extensions.cs'.使用“path\Extensions.cs”中定义的类型。 path\main.cs路径\main.cs

What is wrong with my project?我的项目有什么问题? How to get rid of the warning?如何摆脱警告?

The code of my project has the following structure:我的项目代码结构如下:

Extensions.cs扩展.cs

namespace Extensions
{

    public class MessageDetails
    {
        public string message { get; set; }
        public string link { get; set; }
        public string picture { get; set; }
        public string name { get; set; }
        public string caption { get; set; }
        public string description { get; set; }
        public string userid { get; set; }
        public string username { get; set; }

        public object actions { get; set; }
        public object privacy { get; set; }
        public object targeting { get; set; }
    }

}

lib.dll lib.dll

namespace MyClassLib {

    public class MyClassLibFoo {
        public void foo(MessageDetails parameters) {
            /* .. */
        }
    }

}

main.cs主文件

using MyClassLib;
using Extensions;

class Program
{
    static void Main(string[] args)
    {
        MessageDetails md = new MessageDetails();
    }
}

In my case, with Visual Studio 2013, I found that one of my class libraries had developed a reference to itself. 在我的例子中,使用Visual Studio 2013,我发现我的一个类库已经开发了一个对它自己的引用。 I think it happened when I added a new project to my solution or it was a bug, but either way it was causing this exact issue. 我认为这是在我向我的解决方案添加新项目时发生的,或者它是一个错误,但无论哪种方式都导致了这个问题。

Check your project references for any circular references. 检查项目参考是否有任何循环引用。

It seems like Extensions.cs is both part of the project that builds lib.dll and your main.exe 看起来Extensions.cs既是构建lib.dll和你的main.exe的项目的一部分

Remove it from one of the project to fix this issue. 从其中一个项目中删除它以解决此问题。

I had this kind of issue where I had reverted from a target .NET Framework version of 4.5.2 to 4.0 . 我遇到过这种问题,我已经从目标.NET Framework 4.5.2版恢复到4.0

Classes in my App_Code folder had methods that called methods in other classes in that folder. 我的App_Code文件夹中的类具有在该文件夹中的其他类中调用方法的方法。 When I created a standard folder I named "AppCode", and moved my classes into it, I no longer had the issue. 当我创建一个名为“AppCode”的标准文件夹,并将我的类移入其中时,我不再遇到问题。

If I re-created the "App_Code" folder and move my classes back into it, I will have this issue again. 如果我重新创建“App_Code”文件夹并将我的类移回其中,我将再次遇到此问题。 I'm convinced it has to do with my .NET Framework version or that Visual Studio just doesn't deal well with changing it after being initially built/targeted to another version. 我确信它与我的.NET Framework版本有关,或者Visual Studio在最初构建/定位到另一个版本之后不能很好地改变它。

You can't have two copies of the extensions class, even though the code is the same they are not seen as the same object. 您不能拥有扩展类的两个副本,即使代码相同,它们也不会被视为同一个对象。 Both your dll and main application will need to reference the exact same one. 您的dll和主应用程序都需要引用完全相同的应用程序。

You could try creating a 'Common Files' class library and add the extensions class to it, that way you will always be using the correct class 您可以尝试创建“公共文件”类库并向其添加扩展类,这样您将始终使用正确的类

I had this problem with a project that is also hosted on NuGet. 我在一个也在NuGet上托管的项目遇到了这个问题。 I checked all project references. 我检查了所有项目参考。 Finally, the object browser revealed that the DLL of an older version of my NuGet package was somehow loaded in Visual Studio from the NuGet cache folder ("C:\\Users\\{username}\\.nuget\\packages"). 最后,对象浏览器显示我的NuGet包的旧版本的DLL以某种方式从NuGet缓存文件夹(“C:\\ Users \\ {username} \\。nuget \\ packages”)加载到Visual Studio中。 I removed the package from the cache folder, it disappeared from the object browser and everything was working fine again. 我从缓存文件夹中删除了包,它从对象浏览器中消失了,一切都恢复正常。

I had a Shared Project, "Project A," which was included in both "Project B" and "Project C." 我有一个共享项目,“项目A”,它包含在“项目B”和“项目C”中。

"Project A" was added as a Shared Project in "Project B" and "Project C." “项目A”被添加为“项目B”和“项目C”中的共享项目。

"Project A" also included a traditional reference to "Project B." “项目A”还包括对“项目B”的传统参考。

To correct the problem, I removed the reference to "Project B" from "Project A." 为了解决这个问题,我从“项目A”中删除了对“项目B”的引用。

I had faced same problem. 我遇到过同样的问题。 Just a simple solution for that. 只是一个简单的解决方案。

Check your project references there must be same project reference. 检查项目引用必须有相同的项目参考。 just remove that, it will work. 只是删除它,它会工作。

sometimes I get this error - it's a bug though in my case.. All I have to do to fix it is change the first letter of my script file name from upper case to lowercase in the file in Explorer / (or in Unity Engine in my case) and then change the name / class accordingly in my script. 有时我得到这个错误 - 虽然在我的情况下这是一个错误..所有我需要做的就是修改它是将我的脚本文件名的第一个字母从大写改为小写在Explorer /(或在Unity Engine中)我的情况)然后在我的脚本中相应地更改名称/类。 Idk why this happens.. just does - and Idk why this fix works .. but in my case it always does. Idk为什么会发生这种情况..只是 - 并且Idk为什么这个修复工作..但在我的情况下它总是这​​样。 - Otherwise you probably have 2 copies of the same script / same class name for 2 diff scripts. - 否则,对于2个差异脚本,您可能有2个相同脚本/相同类名的副本。 Hope this helps. 希望这可以帮助。

After reading through many answers on SO the solution was still unclear.在阅读了许多关于 SO 的答案后,解决方案仍然不清楚。 My situation was similar but the solution was found by:我的情况类似,但通过以下方式找到了解决方案:

Example project Name: My.Example.Project示例项目名称:My.Example.Project

  1. Opening my project打开我的项目
  2. Open the References dropdown打开参考下拉列表
  3. Finding My.Example.Project in the References section在参考部分找到 My.Example.Project
  4. Deleting the reference to My.Example.Project删除对 My.Example.Project 的引用

That fixed it!那解决了它!

If you really need to have both classes declared or referenced in two separate dll, you can mark your class as internal . 如果您确实需要在两个单独的dll中声明或引用这两个类,则可以将您的类标记为internal

Internal types or members are accessible only within files in the same assembly, therefore it will prevent the collision. 内部类型或成员只能在同一程序集中的文件中访问,因此它可以防止冲突。

I fixed this error by deleting the.suo file in the directory sturcture vs directory.我通过删除目录结构 vs 目录中的 .suo 文件来修复此错误。 stop vs then delete restart vs. that worked for me.停止 vs 然后删除重新启动 vs. 对我有用。

暂无
暂无

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

相关问题 类型X与导入的类型X冲突 - The type X conflicts with imported type X 如何处理'File1.cs'中的'Foo'类型与'File2.dll'中导入的类型'Foo'警告冲突 - How to deal with The type 'Foo' in 'File1.cs' conflicts with the imported type 'Foo' in 'File2.dll' warning CS0436:类型与导入的类型冲突 - CS0436: Type conflicts with the imported type 如果将同一个文件包含到两个项目中而忽略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? 'YZ'类型中不存在类型'X' - The type 'X' does not exist in the type 'Y.Z' 类型____与导入的类型冲突 - The type ____ conflicts with imported type 无法解析“ xyz”类型的服务 - Could not resolve a service of type 'x.y.z' 类型与导入的名称空间冲突 - The type conflicts with imported namespace VS 2015共享项目导致X程序集和Y程序集中都存在类型(CS0433) - VS 2015 Shared Project causes Type exists in both X assembly and Y assembly (CS0433) “directory\class.aspx.cs”中的类型“class”与“app”中导入的类型“class”冲突。 使用“directory\class.aspx.cs”中定义的类型 - The type 'class' in 'directory\class.aspx.cs' conflicts with the imported type 'class' in 'app'. Using type defined in 'directory\class.aspx.cs'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM