Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do I need to reference this assembly, even though its not being used

I've got an architecture like the following:

Data (Class Library that handles our Entity Framework stuff)
Components (Middle tier class library that references Data library)
WebOffice (Web Application that references Components library, but NOT Data library)

Now, I have the following snippet of code (this lives inside our Components.Payment.cs; and tblPayment is contained in our Data library.):

    public static Payment Retrieve(int id)
    {
        var t = repository.Retrieve(id);
        //the above line returns a tblPayment object
        if (t != null)
            return new Payment(t);
        return null;
    }


    public static Payment Retrieve(tblPayment tblPayment)
    {
        return new Payment(tblPayment);
    }

After I added this; the WebOffice project is giving the following error:
errorCS0012: The type 'Data.Model.tblPayment' is defined in an assembly that is not referenced. You must add a reference to assembly 'Data, Version=3.5.0.0, Culture=neutral, PublicKeyToken=749b8697f3214861'.

Now, this doesn't quite make sense to me, because the WebOffice project isn't calling the Retrieve(tblPayment tblPayment) method at all. (That's only being used inside the Components library)

Any clue why it would be asking for the Data reference? Do I need to reference every library that a referenced library references? (try saying that 5 times fast...)

like image 788
Jim B Avatar asked Jul 08 '10 18:07

Jim B


2 Answers

The general rule here is that a reference to the containing assembly of any type in the public interface of another assembly must be added to the project. Otherwise the compiler does not know how to resolve that type.

To answer your second question, you do not need to add references to assemblies that contain types which are only used internally to other assemblies.

like image 53
Brian Gideon Avatar answered Oct 09 '22 11:10

Brian Gideon


The compiler needs to know what tblPayment is in order to perform overload resolution on the Resolve method.

like image 2
SLaks Avatar answered Oct 09 '22 10:10

SLaks