简体   繁体   中英

C# Getting Error: Type “Indirect.Class” Not Defined In an Assembly Not Referenced . Must Add a reference to assembly “Indirect”

I realize this question has been brought up, but mine is being caused for different reasons than i've seen on the similar questions, so here is my setup.

I have 2 c# projects, A and B , and project B reference a third party library, Indirect . A will call a static method defined in B , and when I build I get the following error.

Error 2 The type 'Indirect.Class' is defined in an assembly that is not referenced. You must add a reference to assembly 'Indirect, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'.

Here is the code:

//Project A
class A
{
    static void Main(string[] args)
    {
        B.TestMethod("fileName", "param2");
    }
}

//Project B
public class B
{
    public static string TestMethod(string fileName, string param2)
    {
        return "";
    }

    public static bool TestMethod(Indirect.Class doc, string anotherParam)
    {
        return false;
    }
}

So even though in A I am calling the B method that does not depend on the Indirect assembly, I still see the error. Can someone explain to me why the compiler is having trouble determining this at compile time? I could simply rename the second B.TestMethod and the error would go away, or I could add a third parameter to it, but all of this seems unnecessary.

The compiler has to pick the best matching overload, and in doing so, it evaluates the number of conversions necessary for each candidate and then ranks them.

You and I know that because there is an exact match, it doesn't matter how bad the others are, because they can't ever win. But that's not how the language rules are written, nor how the compiler works.

The compiler doesn't know you aren't calling the second overload until it ranks all the overloads, including the one you aren't calling. To do that, it checks whether the actual parameter (of type string ) can be converted to the type of the formal parameter ( Indirect.Class ), and it needs to look inside the assembly for Indirect.Class because that's where such a conversion would be defined.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM