1

I have two different assemblies: Assembly1 and Assembly2. Both assemblies use the same namespace 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; I'll call this class 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. 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

pavuxun
  • 411
  • 2
  • 15
  • Why not simply try it out? IMHO it smells if two classes have identical names (and even identical namespaces) but do different things. – MakePeaceGreatAgain Aug 07 '18 at 14:57
  • [`extern alias`](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/extern-alias). There are features that help you deal with these problems, but usually it's a sign that you're not picking good enough names. I.e. what actually *distinguishes* a `PackageWriter` that uses assembly1 from one that uses assembly2, and why isn't that part of the name? – Damien_The_Unbeliever Aug 07 '18 at 15:03
  • If they have *"identical functionality but different implementation"*, then why not just pick the one with the better implementation and use that in both places? – Rufus L Aug 07 '18 at 15:34
  • @RufusL - if one should in fact be named `XmlPackageWriter` and the other `ASN1PackageWriter`, then neither is necessarily better than the other, but as I said above, it points to a naming failure. – Damien_The_Unbeliever Aug 07 '18 at 16:21
  • @Damien_The_Unbeliever I guess I don't understand why you'd have two different methods in the same namespace that have **identical** functionality. Maybe it should be phrased *similar* functionality? – Rufus L Aug 07 '18 at 17:40
  • @RufusL I have had to do this before when I needed to use 2 different versions of an API. It's not ideal, but it happens. However, I've never found myself purposefully writing code this way as it appears is being done here. This absolutely smells, and begs for the use of a Factory pattern instead of this swapping assemblies nonsense. – TheSoftwareJedi Aug 07 '18 at 17:47
  • @RufusL - because many people here describe things as being "identical" until you dig down into it and what they mean is in fact "superficially similar" but doing completely different things. Unless the OP wishes to contradict me. – Damien_The_Unbeliever Aug 07 '18 at 17:57

2 Answers2

3

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. 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

This is perhaps needed when running 2 versions of an API or similar, although I maintain to avoid this if at all possible.

For more detail, see this answer.

TheSoftwareJedi
  • 34,421
  • 21
  • 109
  • 151
0

If it's within the same namespace then NO unless you are declaring it as partial class

Rahul
  • 76,197
  • 13
  • 71
  • 125