简体   繁体   English

如何防止在辅助类中引用的未使用的 dll 被复制到项目中?

[英]How to prevent unused dll's referenced in helper class to be copied to project?

I have this class library called 'Helpers' where I put some re-usable code in. Most of this code is used in most of my projects and usually there are no special requirements.我有一个名为“Helpers”的类库,我在其中放置了一些可重用的代码。这些代码的大部分用于我的大多数项目中,通常没有特殊要求。

A while back I had some re-usable SharePoint code that required me to add a reference.不久前,我有一些可重用的 SharePoint 代码,需要我添加引用。

using Microsoft.SharePoint.Client;
namespace Helpers
{
    public static class SharePoint
    {
        public static List<ListItem> GetItems(List list, CamlQuery query = null)
        {...}
    }
}

Every class has it's own file but resides in the same namespace 'Helpers'.每个类都有自己的文件,但驻留在同一个命名空间“Helpers”中。 Only this SharePoint class has the 'using Microsoft.SharePoint.Client'只有这个 SharePoint 类具有“使用 Microsoft.SharePoint.Client”

A project that references my Helper library now holds Microsoft.SharePoint.Client.dll.引用我的 Helper 库的项目现在包含 Microsoft.SharePoint.Client.dll。

Even when this class is seperated into another namespace, it's copied.即使这个类被分离到另一个命名空间中,它也会被复制。 There is a Copy Local switch on the reference but this will prevent the dll to be copied even when used.引用上有一个 Copy Local 开关,但这会阻止 dll 被复制,即使在使用时也是如此。

Is there a simple way to prevent this dll to be copied locally when not used and copied locally when used?有没有一种简单的方法来防止这种情况的dll本地复制时,使用时不使用复制本地?

When you create a library you have to think of it as a public API.创建库时,您必须将其视为公共 API。 That library has no idea what functions/classes that are in it are going to be used by the calling library or not.该库不知道调用库将使用或不使用其中的哪些函数/类。 In your case, it sounds like both the library and the caller are in the same solution so you're thought is "why can't it check what functions I'm using?"在您的情况下,听起来库和调用者都在同一个解决方案中,所以您认为“为什么它不能检查我正在使用的功能?” Well that's incredibly difficult, if not impossible to do (I'm leaning toward impossible).嗯,这非常困难,如果不是不可能的话(我倾向于不可能)。 Why?为什么? What if I have a program that is a console application that asks the user "what function do you want me to execute?"如果我有一个作为控制台应用程序的程序,它会询问用户“您希望我执行什么功能?” The user types the name "Helpers.SharePoint.GetItems({"a","b","c"}, null)"用户键入名称“Helpers.SharePoint.GetItems({"a","b","c"}, null)"

Then through some rather complex parsing and Reflection, you call this method.然后通过一些相当复杂的解析和反射,你调用这个方法。 How would Visual Studio know that at compile time? Visual Studio 如何在编译时知道这一点? It wouldn't have a clue since it's 100% user driven.它不会有任何线索,因为它是 100% 用户驱动的。 It's an obscure example for sure, but it's an example of why the compiler has no idea whether or not you might need that library.这肯定是一个晦涩的例子,但它是为什么编译器不知道您是否可能需要该库的一个例子。

As a result, it's going to copy it for you since it can't know for sure what is needed and what isn't.因此,它会为您复制它,因为它无法确定需要什么,不需要什么。 It believes it's needed because you told it it was when the reference was added.它认为它是必要的,因为你告诉它它是在添加引用时。

If you want the dll to be removed, you can create a post build step to manually delete it after doing a build, but Visual Studio isn't going to do it for you and that's a good thing.如果您希望删除 dll,您可以创建一个构建后步骤以在构建后手动删除它,但 Visual Studio 不会为您执行此操作,这是一件好事。 Personally, I'd say just let the dll exist.就个人而言,我会说只是让 dll 存在。 The only harm is disk space and the maybe 500KB that dll takes up is pretty much of no importance in the world of 2TB hard drives!唯一的危害是磁盘空间,而 dll 可能占用的 500KB 在 2TB 硬盘驱动器的世界中几乎不重要!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM