开发者

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...)


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.


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


You can't resolve the public interface for the library without information about the parameters to all of its functions. If you're referencing a library wherein a public method on a public type accepts a parameter of type X, you need to know what X is, regardless of whether you're currently using that method or not.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜