[英]when project needs reference to other dlls
I encountered strange behavior and don't know the reason. 我遇到了奇怪的行为,不知道原因。 I have two projects A and B. A references System.Web assembly and B references A. I add using statement to projects B class
using A
. 我有两个项目A和B. A引用System.Web程序集和B引用A.我添加using语句项目B级
using A
。 It works fine until I start using linq statements. 它工作正常,直到我开始使用linq语句。 I get compilation error :
我收到编译错误 :
The type 'System.Web.UI.Control' is defined in an assembly that is not referenced. You must add a reference to assembly 'System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'.
Simplified example project. 简化的示例项目。 Two projects Parent and Child.
两个项目父母和孩子。 Parent project references System.Web.
父项目引用System.Web。 Child project references Parent project.
子项目引用父项目。 Parent project contains one class Parent:
父项目包含一个类Parent:
using System.Web.UI;
namespace Parent
{
public static class Parent
{
public static T DoStuff<T>(this object obj)
where T : Control
{
return null;
}
}
}
Child project contains one class Child: 子项目包含一个类Child:
using System.Collections.Generic;
using System.Linq;
using Parent;
namespace Child
{
public class Child
{
void Test()
{
var strings = new List<string>();
var string1 = strings.FirstOrDefault();
}
}
}
Exception is thrown on var string1 = strings.First();
var string1 = strings.First();
抛出异常var string1 = strings.First();
. 。 I would just add reference to System.Web in Child project and everything works fine.
我只想在Child项目中添加对System.Web的引用,一切正常。 But I would like to understand why it works like this.
但我想理解为什么它会像这样工作。 If I change parent class to:
如果我将父类更改为:
public static class Parent
{
public static Control DoStuff(this object obj)
{
return null;
}
}
Instead of using generics I use Control class directly. 我没有使用泛型,而是直接使用Control类。 And it works fine.
它工作正常。 Any ideas?
有任何想法吗?
edited: var string1 = strings.FirstOrDefault();
编辑:
var string1 = strings.FirstOrDefault();
If you reference a project, it adds a reference to its compiled dll. 如果引用项目,则会添加对其编译的dll的引用。 However, only because you reference a dll doesn't makes your project to reference recursively all dll's that are referenced by the referenced dll.
但是,仅仅因为您引用dll不会使您的项目以递归方式引用引用的dll引用的所有dll。
Let's say you add a reference to project B to your project A. While project B has a reference to System.Web
it won't automatically reference it in project A. If the system would recursively add references to a dll you would end up having a ton of references you don't need. 假设您将项目B的引用添加到项目A.虽然项目B有对
System.Web
的引用,但它不会在项目A中自动引用它。如果系统递归地添加对dll的引用,您最终会有大量你不需要的参考资料。
That is why you still have to reference all necessary assemblies manually in your project A in order to be able to use everything from Project B. 这就是为什么你仍然需要在项目A中手动引用所有必要的程序集,以便能够使用项目B中的所有内容。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.