pdm2011 pdm2011 - 3 months ago 22
C# Question

CS0436: Type conflicts with the imported type

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?

Answer

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.

Comments