As Damien_The_Unbeliever already pointed out in the comments assemblies are loaded dynamically whenever a type from it is used. Depending on where a type is used this happens when a Type is loaded (Properties, Fields, MethodParameters ...) or when a method is invoked (local variables, static calls).
In your example if you really want to load another assembly based on a condition I suggest to place it in another method. This way the other assembly is only loaded when GoDeep()
is called.
//first assembly
public void Go()
{
if (condition)
GoDeep();
}
[MethodImpl(MethodImplOptions.NoInlining)]
private void GoDeep()
{
var s = new TestItem(); //at this point second assembly will be loaded
}
Fun fact: This the reason that FileNotFoundExceptions
are not thrown at application startup but sometimes maybe days later when the user finally clicked a button that would have used the missing file.
Edit: To make sure the method is not inlined in Release this was added for the JIT. I want to add I would not use this approach at all because it is, to say the least, risky and may cause application failure.