简体   繁体   English

当项目需要引用其他dll时

[英]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.

相关问题 在项目中“添加为链接”时如何引用dll? - How do I reference dlls when “added as link” in project? 当我在Visual Studio中创建项目引用时,我是否必须在部署时包含这些项目的DLL? - When I make a project reference in Visual Studio, do I then have to include the DLLs for those projects when I deploy? 尝试将其引用到其他项目中时出现dnlib库问题 - dnlib library issue when trying to reference it into other project 在运行时引用.dll? - Reference .dlls on runtime? 为什么一个.net库需要引用其他.net库的依赖关系 - Why one .net library needs the reference of the dependency of other .net library 项目引用另一个项目的obj文件夹中的dll-有时有时不编译 - Project refers to dlls in another project's obj folder - compiles sometimes other times not 如何在c#asp.net中为一个引用其他dll的类创建dll? - How to create a dll for one class that reference other dlls in c# asp.net? 我可以拆分.net DLL而不必重新编译引用原始DLL的其他DLL吗? - Can I split a .net DLL without having to recompile other DLLs that reference the original one? 在直接引用其他dll的其他dll中调用方法会在原始调用中导致引用错误 - Calling a method in a different dll that directly references other dlls causes reference error in the originating call Visual Studio清洁解决方案不会删除所有已移动的dll /项目引用 - Visual Studio clean solution doesn't delete all dlls / project reference getting moved
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM