Why do unused overloads require a project reference?

I'm looking for an explanation as to why unused constructor overloads containing an unacceptable class would generate a compile-time error when using other constructors in that class would be correctly instantiated and executed.

Here is some sample code that demonstrates the problem.

// Console Project - references CL1 but not CL2
static void Main(String[] args)
{
    var rc1 = new ReferencedClass(); // compiles and runs
    var rc2 = new ReferencedClass(1); // wont compile. Needs CL2 reference
    var rc3 = new ReferencedClass(0, ""); // compiles and runs
}

// Class Library 1 (CL1) - references CL2
public class ReferencedClass
{
    private UnreferencedClass _unreferencedClass;
    public ReferencedClass()
    {
        _unreferencedClass = new UnreferencedClass();
    }

    public ReferencedClass(Int32 id)
    {
        _unreferencedClass = new UnreferencedClass();           
    }

    public ReferencedClass(UnreferencedClass uf)
    {
        _unreferencedClass = uf;
    }

    public ReferencedClass(Int32 id, String name)
    {
        _unreferencedClass = new UnreferencedClass();
    }
}

// Class Library 2 (CL2)
public class UnreferencedClass {}

      

In all cases, the constructor with the unaccepted class is not used, so why is the assembly failing in the Int32

overloaded constructor?

+3


source to share


2 answers


The compiler needs a type during overload resolution.

See it here: https://connect.microsoft.com/VisualStudio/feedback/details/817276/error-cs0012-the-type-is-defined-in-an-assembly-that-is-not-referenced-issued-for -an-extension-method-that-is-not-used



Paraphrased answer to link link:

In the process of assigning a value to the name "ReferencedClass" in your program, the compiler follows the search rules to determine the set of methods it should learn. Both constructors take one argument and are treated as part of overload resolution. To determine what makes sense for the call, the compiler must understand all types in constructor signatures. If some types are not found due to missing references, the compiler doesn't just drop the candidate, but instead needs the user to put in the missing references.

+3


source


It looks like your example code is incomplete. As you quote here, it does not reproduce the error.

It reproduces if

  • You had another UnitOfWork class that also had a Context property, but of a different type, and
  • You have used UnitOfWork.Context in ProjectA.

Same:

namespace SomeNameSpace
{
    public class UnitOfWork
    {
        public UnitOfWork()
        {
        }
        public string Context { get; set; }
    }
}

      

Then in ProjectA this will compile fine:



using SomeNamespace;

//...

public void MyFunc()
{
    var unitOfWork = new UnitOfWork();
    // ...
    var context = unitOfWork.Context;
    // ...
}

      

Even this would compile just fine:

using ProjectB;

//...

public void MyFunc()
{
    var unitOfWork = new UnitOfWork(1);
    // ...
}

      

But this will require an additional link:

using ProjectB;

//...

public void MyFunc()
{
    var unitOfWork = new UnitOfWork(1);
    // ...
    var context = unitOfWork.Context;
    // ...
}

      

0


source







All Articles