Reputation: 1118
Is there such a thing as delay loading a dll in C#? I know this is possible to do in C++, but what about managed code?
Upvotes: 9
Views: 6194
Reputation: 13007
This article explains in detail how it works in .NET. Summary of key points:
There are a number of different ways that assemblies are loaded in .NET. When you create a typical project, assemblies usually come from:
The Assembly reference list of the top level 'executable' project
The Assembly references of referenced projects
Dynamically loaded assemblies, using runtime loading via AppDomain or Reflection loading
and
.NET automatically loads mscorlib (most of the System namespace) as part of the .NET runtime hosting process that hoists up the .NET runtime in EXE apps, or some other kind of runtime hosting environment (runtime hosting in servers like IIS, SQL Server or COM Interop).
and
Dependent Assembly References are not pre-loaded when an application starts (by default)
Dependent Assemblies that are not referenced by executing code are never loaded
Dependent Assemblies are just in time loaded when first referenced in code
Once Assemblies are loaded they can never be unloaded, unless the AppDomain that hosts them is unloaded.
Upvotes: 2
Reputation: 2099
Yes it is. You don't include the DLL as a reference in your project and where you want to load/use it, you call the Assembly.LoadFile method.
This blog post does a pretty good job with code to describe how to do it.
Upvotes: 1
Reputation: 117260
.NET does that automatically, everything is loaded on demand by default.
Upvotes: 14