Reputation: 5201
I have a class library project in .net say "A" which when built will create a dll namely "A.dll". I have another project say "B" and this project contains the reference of the dll "A.dll". What exactly happens when I add the reference of A in B? Can anybody please tell me what happens both during compile time as well as during runtime?
Upvotes: 3
Views: 860
Reputation: 808
It's not clear what level of detail you are looking for, but at a very basic level the compiler will detect if you are actually using any code from A.dll. If not, the fact that you added an assembly reference does not have any impact on the generated code.
If you are using code from A.dll, the compiler will add a reference to A.dll in the manifest for the assembly generated by project B. If you look at the generated manifest (using a tool like ildasm.exe) you will see the reference. Something like this:
.assembly extern A
{
.ver 1:0:0:0
}
At runtime the .net assembly loader will ensure that A.dll gets loaded as needed, using a variety of rules to try and find an appropriate copy of A.dll (looking in the Global Assembly Cache, probing local directories, etc.) The loader will take care of actually loading the referenced assembly into memory and doing any necessary work to ensure the code can actually be called.
Upvotes: 3
Reputation: 1062600
A.dll is used by the compiler, in combination with any using directives and aliases, to perform type resolution. When those types are found, a marker to the type in A.dll is included in the IL. If A.dll is not used at all the reference is silently dropped.
No code is copied - only qualified names etc - hence you still need to deploy A.dll alongside your B.dll (or in the GAC).
At runtime, when the marker to a type in A.dll is found, "fusion" attempts to load and verify A.dll (there are complex rules and optional indirections here) - once loaded it resolves the type specified and continues (using the type from A.dll).
Upvotes: 9