C# 类型存在于 2 个程序集中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9194495/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Type exists in 2 assemblies
提问by Kou S Hal
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, COMMONTYPEis now exposed through the two Interop assemblies as well.
我从两个不同的第三方 COM DLL 创建了两个 .NET Interop 程序集。这两个 COM DLL 都包含一个名为COMMONTYPE. 因此,COMMONTYPE现在也通过两个 Interop 程序集公开。
I have a third project that needs to use these two Interop assemblies, and I get the infamous compile time error:
我有第三个项目需要使用这两个 Interop 程序集,并且出现了臭名昭著的编译时错误:
The type
<ABC>exists in both<ASSEMBLY1.dll>and<ASSEMBLY2.dll>
该类型同时
<ABC>存在于<ASSEMBLY1.dll>和<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=falseworkaround. What can I do?
由于 COM DLL 是由第三方供应商提供的,我无法访问源代码,而且我正在编写 C# 控制台应用程序,这意味着我没有可以添加debug=false解决方法的web.config 文件。我能做什么?
回答by Tigran
Maybe you can trick it, by changing a namespaceof one of the assemblies, in this case fully qualified name of one COMMONTYPEwill not be equal to another, and possiblyit could resolve your problem with conflict occurring in the 3rd DLL.
也许您可以通过更改namespace其中一个程序集的a 来欺骗它,在这种情况下,一个程序集的完全限定名称COMMONTYPE将不等于另一个程序集,并且可能它可以解决您在第三个 DLL 中发生冲突的问题。
Hope this helps.
希望这可以帮助。
回答by Joel Etherton
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:
除非供应商的命名空间相同(不太可能),否则类型定义实际上将在那时分开。你需要做的(有时这是一个完整的 PITA)是在你的 using 语句中创建一个命名空间别名,而不是简单地应用语句全权委托。这将允许您重新识别命名空间:
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.
这将意味着为这些供应商的每个命名空间中的所有类型使用特定的别名。
回答by Igarioshka
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; }
}
}
回答by Tom
I know this is old, but there's an easier way than the listed. This works when you reference two assembliesthat share types with the exactsame 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"
如果您右键单击对您的 DLL 的引用并选择属性,您将看到这里有一个名为“别名”的属性
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".
默认值为"global"。对于冲突的程序集之一,将此更改为任何其他值。在下面的示例中,我已将其从“全局”更改为“目的地”。
Next, in your code file, you will have to use the extern keywordto use this alias as the root-level namespacefor these types. In this example, you would place the following at the top of your .cs file:
接下来,在您的代码文件中,您必须使用extern 关键字将此别名用作这些类型的根级命名空间。在此示例中,您会将以下内容放在 .cs 文件的顶部:
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();
}
}
}
回答by Zunair
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:
老问题,但找到了一个更简单的选项......选择你想要使用的引用......在属性下,将别名更改为'xyz'现在在代码行中,在顶部添加:
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();


