问题
Out of curiosity, I've created 2 assemblies which both have a class (Class1) with the exact same namespace (Library1). I then create another client referencing those 2 assemblies and try to create an instance of Class1.
The compiler, not surprisingly, gives me a compile-error about the ambiguous reference. Is there any way to explicitly specify the type in the assembly I want to use to avoid the ambiguity?
Note: I know this rarely, if ever at all, happens in practice. It's just a question out of curiosity about language feature.
回答1:
I think you should use an extern alias to wrap the assembly namespaces outside of the Global namespace. Here's how:
In the project that references the 2 assemblies, select one of them under References, and in the Properties window change the Aliases value from
globalto, say,global, Library1a.Do the same for the the other reference, but give it a different alias-- let's go with
global, Library1bfor our example.Insert
extern alias Library1a;and/orextern alias Library1b;as the first 2 lines of any classes that consume the assemblies.When accessing ambiguous members, qualify the namespace with
Library1a.orLibrary1b.. Examples:Library1a.Library1.Class1...Library1b.Library1.Class1...
回答2:
It happens in practice and is a real pain, the external alias can't always solve the problem. Here's one example of where it's an issue Duplicate Namepsaces.
来源:https://stackoverflow.com/questions/1067550/use-types-of-same-name-namespace-in-2-net-assemblies