为什么我需要引用这个程序集,即使它没有被使用
我有一个如下的架构:
数据(处理我们的实体框架内容的类库)
组件(引用数据库的中间层类库)
WebOffice(引用组件库但不引用数据库的 Web 应用程序)
现在,我有以下代码片段(它位于我们的 Components.Payment.cs 内;tblPayment 包含在我们的数据库中。):
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);
}
添加后; WebOffice 项目出现以下错误:
errorCS0012:类型“Data.Model.tblPayment”是在未引用的程序集中定义的。您必须添加对程序集“Data,Version=3.5.0.0,Culture=neutral,PublicKeyToken=749b8697f3214861”的引用。
现在,这对我来说不太有意义,因为 WebOffice 项目根本没有调用 Retrieve(tblPayment tblPayment) 方法。 (这仅在组件库中使用)
有任何线索为什么它会要求数据引用吗?我是否需要引用被引用库引用的每个库? (试着快说5倍...)
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...)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这里的一般规则是,必须将对另一个程序集的公共接口中任何类型的包含程序集的引用添加到项目中。否则编译器不知道如何解析该类型。
要回答第二个问题,您不需要添加对包含仅其他程序集内部使用的类型的程序集的引用。
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.
编译器需要知道
tblPayment
是什么,以便对Resolve
方法执行重载解析。The compiler needs to know what
tblPayment
is in order to perform overload resolution on theResolve
method.如果没有有关库的所有函数的参数的信息,您就无法解析库的公共接口。如果您正在引用一个库,其中公共类型上的公共方法接受类型 X 的参数,则您需要知道 X 是什么,无论您当前是否正在使用该方法。
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.