CopyLocal 之痛

     怪事年年有,今年特别多。

     今天,我机子上好好的一个vs 2008的项目,拷到同事的机子上后,竟然不能运行,报“未能加载程序集”的错误。一看才知道,所有的程序集引用(除了System下的)的“复制本地”属性,都为false。奇了怪了,在我这里不是好好的true吗?

     无奈,卸载项目,然后编辑csproj文件,发现每个引用下面,其实根本就没有指定CopyLocal属性!

image

     重新加载项目,然后把它设成true,然后再打开csproj文件,发现多出来一个节点:

image

     看来Private就是用来指定CopyLocal的值的了。如果没这个节点的话,vs会有一个默认值。这个默认值怎么决定的?查到msdn上关于CopyLocal的描述如下:

   The project-assigned value of CopyLocal is determined in the following order:

  1. If the reference is another project, called a project-to-project reference, then the value is true.
  2. If the assembly is found in the global assembly cache, the value is false.
  3. As a special case, the value for the mscorlib.dll reference is false.
  4. If the assembly is found in the Framework SDK folder, then the value is false.
  5. Otherwise, the value is true.

     马上按照上面提到的情况去对照自己的项目,但没有一个对的上的,看来文档这东西要搞好也不容易,即使是ms也不例外。后来想到,是否vs有个选项可以设置这个默认值的呢?找了一下也没找到。不过即便找到了,也不是一个好的解决办法,因为你不能奢望别人也去修改这个选项。看来以后自己在添加引用的时候,一定要确保每个引用节点下面有这个<private>true/false</private>了,这岂不是得累死了?以后再打开别人的项目的时候,就算在Solution Explorer里看到某个引用的CopyLocal是true或是false,也不敢确定这就是作者的原意了,就算打开csproj文件去看对应的节点,也不敢确定,因为很有可能那里没有private节点,而你不能保证作者的vs默认的是什么,累啊。

     不知各位有没有发现这个问题呢?

原文地址:https://www.cnblogs.com/default/p/1692856.html