The Mask
The Mask

Reputation: 17427

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:

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();
    }
}

Upvotes: 50

Views: 63436

Answers (12)

Shahzaib Hassan
Shahzaib Hassan

Reputation: 13

If the problem arises while using SharedProject, make sure to declare all types as internal.

Upvotes: 0

dcarl661
dcarl661

Reputation: 322

I fixed this error by deleting the .suo file in the directory sturcture vs directory. stop vs then delete restart vs. that worked for me.

Upvotes: 0

dskinner
dskinner

Reputation: 81

After reading through many answers on SO the solution was still unclear. My situation was similar but the solution was found by:

Example project Name: My.Example.Project

  1. Opening my project
  2. Open the References dropdown
  3. Finding My.Example.Project in the References section
  4. Deleting the reference to My.Example.Project

That fixed it!

Upvotes: 2

parapura rajkumar
parapura rajkumar

Reputation: 24403

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.

Upvotes: 37

vapcguy
vapcguy

Reputation: 7537

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.

Upvotes: 13

Brad Donohoo
Brad Donohoo

Reputation: 11

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. Idk why this happens.. just does - and Idk why this fix works .. but in my case it always does. - Otherwise you probably have 2 copies of the same script / same class name for 2 diff scripts. Hope this helps.

Upvotes: 1

Rahul Nikam
Rahul Nikam

Reputation: 11

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.

Upvotes: 1

Guillermo Hernandez
Guillermo Hernandez

Reputation: 1174

If you really need to have both classes declared or referenced in two separate dll, you can mark your class as internal.

Internal types or members are accessible only within files in the same assembly, therefore it will prevent the collision.

Upvotes: 3

NightShovel
NightShovel

Reputation: 3252

I had a Shared Project, "Project A," which was included in both "Project B" and "Project C."

"Project A" was added as a Shared Project in "Project B" and "Project C."

"Project A" also included a traditional reference to "Project B."

To correct the problem, I removed the reference to "Project B" from "Project A."

Upvotes: 2

SvenEV
SvenEV

Reputation: 123

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.

Upvotes: 2

James Blake
James Blake

Reputation: 1118

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.

Upvotes: 71

Eamonn McEvoy
Eamonn McEvoy

Reputation: 8986

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

Upvotes: 4

Related Questions