[英]Can two classes with the same name be in the same namespace across two different assemblies?
I have two different assemblies: Assembly1
and Assembly2
. 我有两个不同的程序集:
Assembly1
和Assembly2
。 Both assemblies use the same namespace SoftwareDrivers
. 两个程序集使用相同的名称空间
SoftwareDrivers
。 Now, I have two implementations of a class that has identical functionality, just different implementation and dependencies that tie one implementation to Assembly1
and the other to Assembly2
; 现在,我有一个具有相同的功能,只是不同的实现方式,并且配合一个实施依赖一类的两种实现
Assembly1
,另一个Assembly2
; I'll call this class PackageWriter
. 我将此类
PackageWriter
。
It is possibly, and likely, some projects will need to reference both assemblies, so I anticipate there will be an issue with resolving which PackageWriter
to use in that scenario. 有些项目可能并且很可能需要引用两个程序集,因此我预计解决该
PackageWriter
中使用哪个PackageWriter
会有问题。 Is this the case? 是这样吗 If so, is the only way to resolve this issue, without changing the structure of the project, to change the name of
PackageWriter
?c 如果是这样,则是在不更改项目结构的情况下更改
PackageWriter
的名称的唯一解决此问题的PackageWriter
?
Yes they can. 是的他们可以。 If both assemblies are referenced, you will receive an error similar to this:
如果两个程序集都被引用,您将收到类似于以下的错误:
error CS0433: The type 'Testing' exists in both 'MyApp.Domain, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' and 'MyApp.Infrastructure, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'
Note, you should never purposefully put yourself in this situation. 请注意,您绝对不应有目的地陷入这种情况。 However, if you for some reason find yourself in a situation where you have to handle this:
但是,如果由于某种原因而发现自己处于必须处理此情况的情况:
Every assembly has an "alias" in .NET and by default it is global
. 每个程序集在.NET中都有一个“别名”,默认情况下是
global
。 This is usually invisible to the day to day programmer. 对于程序员来说,这通常是不可见的。 You can change the alias by clicking on the reference and changing the Alias in Properties.
您可以通过单击引用并在“属性”中更改别名来更改别名。 You would then reference it by having
extern alias myalias
at the top of your file, and the actual class referenced like this myalias::MyApp.Testing
然后,您可以通过在文件顶部使用
extern alias myalias
引用它,并像myalias::MyApp.Testing
这样引用实际的类。
This is perhaps needed when running 2 versions of an API or similar, although I maintain to avoid this if at all possible. 在运行2个版本的API或类似版本时,可能需要这样做,尽管我会尽量避免这样做。
For more detail, see this answer . 有关更多详细信息,请参见此答案 。
如果它在同一个命名空间中,则否,除非您将其声明为partial
类
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.