问题描述:

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?

网友答案:

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 Propeties, you will see that here's a property called "Aliases"

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();
        }
    }
}
网友答案:

Unless the namespaces of the vendors are identical (unlikely), the type definitions will actually be separate at that point. What you'll need to do (and this is a complete PITA sometimes) is create a namespace alias in your using statement rather than simply applying the statement carte blanche. This will allow you to re-identify the namespaces:

using Vendor1 = Vendor.Namespace;
using Vendor2 = OtherVendor.Namespace;

...

Vendor1.COMMONTYPE blah = new Vendor1.COMMONTYPE();
Vendor2.COMMONTYPE blah2 = new Vendor2.COMMONTYPE();

This will mean using the specific alias for all types located in each namespace for these vendors.

网友答案:

Old question but found an easier option... Select the reference you want to use... Under properties, change the Aliases to 'xyz' Now in code line, at the top add:

extern alias xyz;

then add using:

using xyz.VENDOR2.Type;

or another way of to use using:

using OtherNameSpace = xyz.VENDOR2.Type;

now you should be able to use the reference explicitly as below:

var abc = new xyz.VENDOR2.Type.abc();

or

var abc = new OtherNameSpace.abc();
网友答案:

you can use an aliases to the different namespaces and/or types:

here's how it would look like:

using other = sssssss.a;
namespace ConsoleApplication1
{
    public class a 
    {
        public string ff { get; set; }
    }
    class Program
    {
        static void Main(string[] args)
        {
            other s = new other();
            a b = new a();
        }
    }
}
namespace sssssss 
{

    public class a
    {
        public string ff { get; set; }
    }
}

MSDN

网友答案:

Maybe you can trick it, by changing a namespace of one of the assemblies, in this case fully qualified name of one COMMONTYPE will not be equal to another, and possibly it could resolve your problem with conflict occurring in the 3rd DLL.

Hope this helps.

相关阅读:
Top