Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rules of referenced assemblies copying

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?

like image 471
SiberianGuy Avatar asked Aug 27 '11 14:08

SiberianGuy


1 Answers

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...

like image 53
Jon Skeet Avatar answered Sep 26 '22 13:09

Jon Skeet