Reputation: 43
I've recently encountered a dependecy resolution error that I'm hoping someone here can explain.
I have an interface defined in a 3rdparty assembly (I3rdParty
), one "common" assembly that's depends on that assembly and a "client" library that depends on the "common" assembly.
Let's call them, 3rdparty.dll, common.dll and client.dll.
The client.dll should not have a dependency to the 3rdparty.dll.
in common.dll the following was defined:
public static class Factory
{
public static object Create(I3rdParty ifc) { ... }
public static object Create(string value1, string value2, long? value3 = null) { ... }
}
One of the factory methods was used from the client.dll like:
var instance = Factory.Create("SomeValue", "SomeValue2");
At this point everything worked as expected.
Then a bool parameter was introduced to the first factory method in common.dll so it became:
public static object Create(I3rdParty ifc, bool value) { ... }
Then the build of client.dll started failing due to a missing dependency to 3rdparty.dll, e.g:
The type 'I3rdParty' is defined in an assembly that is not referenced...
I'm assuming that this has something to do with that the methods now accepts the same number of parameters (since the second Create method's third parameter defaults to null).
But I thought that it would still be able to select the correct Create method based on the type of the parameters. Can anyone explain the reason for the behavior I'm seeing?
Upvotes: 4
Views: 66
Reputation: 49013
After you added a bool
parameter to the first overload, the compiler has now to check for two possible method signatures to choose the one that should be used (this is the overload resolution).
You're calling Create(string, string)
With two parameters, you have the following overloads available:
Create(I3rdParty, bool)
Create(string, string)
Obviously only the second one can match (as a string
cannot be implicitly converted to bool
for the second parameter), but it appears the compiler is not clever enough and has to know what exactly I3rdParty
is (which means it requires the reference to the assembly that defines it), before being able to determine the (I3rdParty, bool)
overload wasn't an option.
Upvotes: 1