I have written the following NUnit test;
[Test]
public void Assembly1_ShouldNotReference_Assembly2()
{
var assembly = Assembly.Load("Assembly1");
var referencedAssemblies = assembly.GetReferencedAssemblies();
var hasReference = referencedAssemblies.Any(a => a.Name == "Assembly2");
Assert.IsFalse(hasReference);
}
This always passes, even if I add a reference.
<ItemGroup>
<ProjectReference Include="..Assembly2Assembly2.csproj" />
<ProjectReference Include="..Assembly3Assembly3.csproj" />
</ItemGroup>
When I debug the test and inspect the referenced assemblies I do not see Assembly2 in the list, but Assembly3 is there and changing the test to assert that Assembly3 is not referenced causes it to fail.
Types in Assembly3 are used throughout Assembly1, but Assembly2 only exists as a project reference. Ultimately I want to test that Assembly1 never references Assembly2 but why am I seeing this behaviour?