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
. It works fine until I start using linq statements. 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. Child project references Parent project. Parent project contains one class 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:
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();
. I would just add reference to System.Web in Child project and everything works fine. 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. And it works fine. Any ideas?
edited: var string1 = strings.FirstOrDefault();
If you reference a project, it adds a reference to its compiled 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.
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.
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.