简体   繁体   English

在两个不同的程序集中,两个具有相同名称的类可以位于相同的名称空间中吗?

[英]Can two classes with the same name be in the same namespace across two different assemblies?

I have two different assemblies: Assembly1 and Assembly2 . 我有两个不同的程序集: Assembly1Assembly2 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.

相关问题 两个具有相同名称,相同版本但具有不同公钥的程序集 - Two assemblies with same name ,same version but different public key 可以在一个项目中引用两个具有相同名称的不同程序集吗? 不同的二进制文件 - Possible to reference two different assemblies with the same name in a project? different binaries 在两个不同的appdomain中运行两个相同的程序集 - Running two same assemblies in two different appdomains CLR如何在运行时区分不同程序集中具有相同名称但没有名称空间的类? - How does CLR differentiate classes with the same name but without namespace in different assemblies at runtime? 两个带有“同名”的csProj程序集导致问题 - Two csProj assemblies with “same name” causing issues 两个程序集中同名的类(有意) - Class with same name in two assemblies (intentionally) 如何从两个不同的子文件夹将两个版本的相同程序集加载到两个不同的域中? - How can I load two versions of same assemblies into two different domains from two different subfolders? 具有相同名称空间的两个不同的DLL - two different DLL with same namespace 使用两个具有相同名称和相同命名空间的 DLL - Using two DLLs with same name and same namespace 用相同的名称/命名空间实例化不同的类 - Instantiate different classes with the same name/namespace
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM