类型存在于2个程序集中

我创造了两个。NET 互操作程序集来自两个不同的第三方 COM DLL。两个 COMDLL 都包含一个名为 COMMONTYPE的类型。因此,COMMONTYPE现在也通过两个 Interop 程序集公开。

我有第三个项目,需要使用这两个 Interop 程序集,我得到了臭名昭著的编译时错误:

<ABC>类型存在于 <ASSEMBLY1.dll><ASSEMBLY2.dll>

由于 COM dLL 是由第三方供应商提供的,所以我没有访问源代码的权限,而且我正在编写一个 C # 控制台应用,这意味着我没有 web.config 文件可以添加 debug=false解决方案。我能做什么?

79660 次浏览

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.

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.

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

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();
}
}
}

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();

I know this is old but I ran across this recently. The best way I found to fix this is to modify YourProjectName.csproj file by adding the following:

<Target Name="ChangeAliasesOfStrongNameAssemblies" BeforeTargets="FindReferenceAssembliesForReferences;ResolveReferences">
<ItemGroup>
<ReferencePath Condition="'%(FileName)' == 'Namespace.You.Want.To.Alias'">
<Aliases>NewAliasForNamespace</Aliases>
</ReferencePath>
</ItemGroup>
</Target>