问题
There are quite a few questions/answers about the compiler error mentionend below and how to resolve it, but the question here is asking about some insights why in this case this is required.
Why does a project A which uses an overload of a method of another referenced project B, which uses an object of project C in one of it's overloaded signatures require, that you reference project C from project A, even if you never use the object from project C?
I guess it must have to do with the resolving of which overload to use, but I'd like to understand the concept behind.
Here's an example:
Put each of the classes in an own assembly.
//Put into Assembly C
public class C {}
//Put into assembly B, reference C
public class B
{
public static void Test(string param) //Simple method with one string parameter
{
}
public static void Test(C param) //Overload which uses type of assembly C
{
}
}
//Call placed in method of assembly A which uses and references only assembly B, but not C
B.Test("TestString"); // fails to compile, CS0012
CS0012 The type 'C' is defined in an assembly that is not referenced. You must add a reference to assembly 'C, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'.
You can get interesting results when playing around with the overloads of assembly B, because not every combination does produce the error:
public static void Test(){}
public static void Test(C param){}
B.Test(); //Call from assembly A compiles
Another example:
public static void Test(string param){}
public static void Test(C param, int param2){}
B.Test(""); //Call from assembly A compiles
Yet another example:
public static void Test(string param, string param2){}
public static void Test(C param, int param2){}
B.Test("",""); //Fails, CS0012
So this propably has to do how the overload resolution is done. However, I don't understand why the compiler can't just work it's way up the dependency tree and requires me to do a direct reference? Wouldn't the overload resolution have to be done in assembly B anyways?
来源:https://stackoverflow.com/questions/33832414/why-does-the-compiler-when-using-an-overload-in-another-assembly-sometimes-requi