Type exists in 2 assemblies

Kou S Hal picture Kou S Hal · Feb 8, 2012 · Viewed 58.8k times · Source

I have created two .NET Interop assemblies from two different third-party COM DLLs. Both of the COM DLLs contained a type named COMMONTYPE. Therefore, COMMONTYPE is now exposed through the two Interop assemblies as well.

I have a third project that needs to use these two Interop assemblies, and I get the infamous compile time error:

The type <ABC> exists in both <ASSEMBLY1.dll> and <ASSEMBLY2.dll>

Since the COM DLLs are provided by a third-party vendor, I have no access to the source code, and I'm writing a C# Console application, which means I have no web.config file where I could add the debug=false workaround. What can I do?

Answer

Tom picture Tom · Jul 15, 2013

I know this is old, but there's an easier way than the listed. This works when you reference two assemblies that share types with the exact same name and namespace.

If you right-click on the Reference to your DLL and select Properties, you will see that here's a property called "Aliases"

enter image description here

The default value is "global". For one of the conflicting assemblies change this to any other value. In the example below, I've changed it from "global" to "destination".

Next, in your code file, you will have to use the extern keyword to use this alias as the root-level namespace for these types. In this example, you would place the following at the top of your .cs file:

extern alias destination

Now, within this file, you can reference both types.

extern alias destination;
namespace Test
{
    public static class TestClass
    {
        public static void Success()
        {
            var foo = destination::Some.Duplicate.Namespace.SomeDuplicateType();
            var bar = Some.Duplicate.Namespace.SomeDuplicateType();
        }
    }
}