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

56,904

Solution 1

It seems like Extensions.cs is both part of the project that builds lib.dll and your main.exe

Remove it from one of the project to fix this issue.

Solution 2

In my case, with Visual Studio 2013, I found that one of my class libraries had developed a reference to itself. 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.

Solution 3

I had this kind of issue where I had reverted from a target .NET Framework version of 4.5.2 to 4.0.

Classes in my App_Code folder had methods that called methods in other classes in that folder. When I created a standard folder I named "AppCode", and moved my classes into it, I no longer had the issue.

If I re-created the "App_Code" folder and move my classes back into it, I will have this issue again. 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.

Solution 4

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.

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

Solution 5

I had this problem with a project that is also hosted on 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"). I removed the package from the cache folder, it disappeared from the object browser and everything was working fine again.

Share:
56,904
The Mask
Author by

The Mask

Updated on July 09, 2022

Comments

  • The Mask
    The Mask almost 2 years

    The main.cs of my project returns the following warning:

    Warning 1 The type 'Extensions.MessageDetails' in 'PATH\Extensions.cs' conflicts with the imported type 'Extensions.MessageDetails' in 'path\lib.dll'. Using the type defined in 'path\Extensions.cs'. path\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

    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

    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();
        }
    }
    
    • Eamonn McEvoy
      Eamonn McEvoy over 12 years
      Are there two copies of your extensions class (one packaged in your dll and one in your main program)?
    • The Mask
      The Mask over 12 years
      yes,I need use the MessageDetails in namespace Extensions class..
    • Eamonn McEvoy
      Eamonn McEvoy over 12 years
      That is your problem, they both need to reference the same one. Try adding it to a class library to do this.
    • BurnsBA
      BurnsBA over 7 years
      This happened to me in an ancient webforms project in Visual Studio 2015 such that code in App_Code was being compiled twice which made it ambiguous.
  • Derek Greer
    Derek Greer over 8 years
    I found this to be the cause of this error for me as well. That is quite strange.
  • Tom
    Tom over 7 years
    Still seems to be an issue in vs 2015 as this was my exact issue.
  • Multinerd
    Multinerd over 7 years
    ty, for some reason, vs added my project as a reference.
  • Trevor
    Trevor about 7 years
    Good answer. Just to add to this first thing to check is your references for the project so expand this in Solution Explorer and you will probably find that your project itself is added as a reference, select it and remove it. My problem occurred beacuse of ReSharper where I had used the refactor tools in various places and somehow managed to add the project as a reference.
  • Niclas
    Niclas almost 6 years
    This happened for me in Unity and VSCode when Duplicating a ScriptableObject, and then renaming the file and the class definition. Restarting VSCode resolved it.
  • Rob
    Rob almost 6 years
    Was going nuts why we had this, never realized was possible!
  • Boltyk
    Boltyk over 5 years
    Exactly what helped me to resolve this strange behavior in VS2017 with framework version 4.5.2. Studio showed crazy warnings I'm using C#6 features in C#5 project, and types in that folder was conflicting with imported themselves, and linq statements was missing something and ReSharper has only the rename refactoring on methods in the class.
  • vapcguy
    vapcguy over 5 years
    @Boltyk Yep - it will lose references to classes.
  • MoustacheDangerous
    MoustacheDangerous about 5 years
    This fixed the issue for me in VS2017 my guess is based on above comments it is ReSharper and spurious Alt-Enter adding references.
  • Peyman Majidi
    Peyman Majidi almost 3 years
    yeap. I don't know how my project referenced itself and causes a bunch of errors