Type exists in 2 assemblies

ε祈祈猫儿з 提交于 2019-11-27 11:52:48
Tom

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

Tigran

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.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!