35

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.

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

"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]".

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.

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

5
  • 2
    Is the LicenceClass type public? If it's only internal, I'd expect it to be okay... Commented Feb 19, 2013 at 16:13
  • What security risk is there going across module boundaries? A bad practice is still a bad practice even if done intentionally. Licence should be spelled license. Commented Feb 19, 2013 at 16:29
  • Yes, it's a public class, and implemented as a singleton. Commented Feb 19, 2013 at 16:30
  • 4
    Licence = noun; License = verb (...in the UK, anyway) Commented Feb 19, 2013 at 16:31
  • The reason going across boundaries is a risk is that it may be possible to intercept calls at the dll boundary and modify parameters to circumvent the check. The assemblies using licence checking are encrypted and use symbol obfuscation so should be secure internally. Commented Feb 19, 2013 at 16:37

6 Answers 6

22

It is worth noting that another way to get such warnings is by simply setting a project in visual studio to reference itself: References -> Solution -> etc etc (how I figured this gem out is left as an exercise to the reader ...)

Visual Studio will happily comply, only to throw a wall of warnings of the type described by OP during build, which is to be expected (upon reflection) since every single class etc is getting defined twice.

Sign up to request clarification or add additional context in comments.

4 Comments

I had that problem. It's dumb.
Yes, I have had the same issue.
...and also I had the same issue. I had no idea how I ended up that situation in the first placce. Thanks for saving my life!
Thanks for this was working in unity and for some reason all my asset references got all messed up after fixing them I had a bunch of warning. I must have accidental referenced my project with its self.
18

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

  1. Disable the warning in classes that cause CS0436:

    #pragma warning disable 0436
    
  2. Have a separate instance of the class, uniquely named in each client project (undesirable from a maintenance point of view).

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

3 Comments

You said it yourself: internally to the assembly. Option 3: Make the license class internal and this should fix the problem. Much better than a pragma.
Marking the class internal worked for me - thanks Mark.
@MarkPflug I know this is old, but your comment should be an answer, and should be the top rated accepted one.
13

I had a web application I converted from ASP.NET 3.5 to 4.5 when I moved to VS2015. I started seeing this as a warning, but the solution would still compile. There were no circular references, and cleaning the solution and deleting the bin and obj folders didn't help.

It turns out that VS2015 wasn't happy with some of my classes in the App_Code folder. The classes in here had the same namespace as the rest of the web pages in the parent folder. Once I moved these classes out of the App_Code folder and to the top level of the web application, the warnings went away.

7 Comments

Next time you're in Pennsylvania @Andy S. let me know, I owe you a beer! (Thank you, this worked!)
I had the same issue due to App_Code. I renamed it to MySite.BL
I had the same problem, but WHY is this a problem? I have been doing this for years and just now it is a warning?
Same thing happened when I port a VS2008 project to VS2013. Thanks a lot!
This just Happened to me with the LinqToSql class...moving it to the root fixed it
|
2

In .NET Core you can also disable the warning in project.json:

{
  "buildOptions":
  {
    "nowarn":
    [
      "CS0436"
    ]
  }
}

Comments

2

I had this error but not with 2 different classes!
Each new class where in conflict with itself, so obviously I had that CS0436 Error.

After some struggling found out that it was about Mirror Asset that I was using in my multiplayer Unity project. Mirror somehow was including every new class that I make (and inherit from NetworkBehavior).

My external editor was VSCode (visual studio code, solution might also apply to visual studio).

Solution

in
Edit / Preferences / External tools / "Generate .csproj files for:"
I started testing different settings, and this worked for me:
(Not sure if the exact settings work for all, but not having the right files in project, leads to this error. like my case.)

Click Regenerate project files and restart Unity and VSCode after applying these settings (or the setting that suits your project).

1 Comment

if the pic was removed, setting is: "uncheck all, then check Local and Git packages"
0

I've met such a case when removed some source files temporarily and restored them back later. It happens that IDE (Rider in my case) tries to restore the classes so when they were missing it just added the reference to the resulting exe. Evidently, when I restored the files, they look as duplicate. The reference IDE inserted looks like this and it's enough to just remove it to fix:

<Reference Include="AppName, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null">
  <HintPath>bin\x86\Debug\AppName.exe</HintPath>
</Reference>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.