I heard about this one on twitter from David Tchepak the author of NSubstitute. He referred to a question they got on the Google groups for NSubstitute.
The question is this.
1/ Create a new empty project (console application is ok)
2/ Add a single line in Main: Dim x As Castle.DynamicProxy.IProxyTargetAccessor
Obviously this don’t compiles.
3/ Install NuGet package of Castle Windsor (3.1.0)
Now, previous code line already compiles.
4/ Install NuGet package of NSubstitue (1.4.3.0)
‘IProxyTargetAccessor’ is ambiguous in the namespace ‘Castle.DynamicProxy’ error appears.
It is very easy to recreate this with the above steps.
So what is the problem.
NSubstitute has ILMerged a part of the Castle Windsor code into it’s own. And now it has part of that namespace too.
This confuses the compiler.
This is solvable in C#.
If you right-click on the NSubstitute reference you can change the alias of that reference (which is normally global).
And now the code will compile.
In VB.Net there is no such property to be found for a reference.
And adapting the vbproj won’t help either, trust me, I tried 😉
In the csproj file you will see something like this.
xml
<ItemGroup>
<Reference Include="Castle.Core">
<HintPath>..packagesCastle.Core.3.1.0libnet35Castle.Core.dll</HintPath>
</Reference>
<Reference Include="Castle.Windsor">
<HintPath>..packagesCastle.Windsor.3.1.0libnet40Castle.Windsor.dll</HintPath>
<Aliases>castle1</Aliases>
</Reference>
<Reference Include="NSubstitute">
<HintPath>..packagesNSubstitute.1.4.3.0libNET40NSubstitute.dll</HintPath>
<Aliases>nsub</Aliases>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Xml" />
</ItemGroup>
See the Aliases tag on some of the references?
Lets copy that to the vbproj file and compile again.
xml
<ItemGroup>
<Reference Include="Castle.Core">
<HintPath>..packagesCastle.Core.3.1.0libnet35Castle.Core.dll</HintPath>
</Reference>
<Reference Include="Castle.Windsor">
<HintPath>..packagesCastle.Windsor.3.1.0libnet40Castle.Windsor.dll</HintPath>
</Reference>
<Reference Include="NSubstitute">
<HintPath>..packagesNSubstitute.1.4.3.0libNET40NSubstitute.dll</HintPath>
<Aliases>nsub</Aliases>
</Reference>
<Reference Include="System" />
<Reference Include="System.Data" />
<Reference Include="System.Deployment" />
<Reference Include="System.Xml" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
</ItemGroup>
But no, it still won’t compile.
Maybe this is something for VS2020. Who knows.