Rules of referenced assemblies copying

后端 未结 4 1645
南笙
南笙 2021-02-19 02:32

Which rules does VS (msbuild?) follow during solution build? In which cases it will copy indirectly referenced asemblies to output folder and in which not?

4条回答
  •  难免孤独
    2021-02-19 03:19

    I've just been a bit of experimentation, and it looks like any indirectly referenced assembly which has a type directly referenced by code in another assembly will be copied. If there's nothing in code, it won't be. Here's my sample scenario:

    • MainProgram: Console application with a direct reference to DirectAssembly. Code in Main:

      var foo = new DirectAssembly.SampleClass();
      
    • DirectAssembly: Class library with a direct reference to IndirectAssembly. Contains SampleClass:

      public class SampleClass
      {
          // Comment out this line to change the behaviour...
          IndirectAssembly.IndirectClass neverUsed = null;
      
          public SampleClass()
          {
              object x = Activator.CreateInstance("IndirectAssembly",
                                                  "IndirectAssembly.IndirectClass");
      
          }
      }
      
    • IndirectAssembly: Contains a public class IndirectClass with a public parameterless constructor

    As described above, it works because IndirectAssembly is copied into the output folder of MainProgram. If you comment out the line indicated in SampleClass, IndirectAssembly is not copied (even though it's still a reference) and the code will fail at execution time.

    I'm not saying these are all the rules, but they're at least a start...

提交回复
热议问题